A Different Kind of Serverless: A Case Study for SQLite and Whisper.cpp

Rate this content
Bookmark

You can build applications with a local-first focus. In this case study, we dig into how you might use WASM to run Whisper.cpp for Speech-to-text and also use WASM to host a local SQLite database. The only reason we need a network connection at all for this application is to get the initial JS payload and to download the Whisper models from a remote store since they can get quite large. 

This talk has been presented at JSNation US 2024, check out the latest edition of this JavaScript Conference.

FAQ

Wasm, or WebAssembly, is an open standard designed to support any language on any operating system. It enables running compiled code from languages like Rust, C++, or Go in the browser, offering major portability.

WebAssembly allows browser-based applications to run more performant code by compiling languages such as C++ or Rust to Wasm, providing real data structures and memory access, which can be faster and more efficient than JavaScript.

Limitations of using WebAssembly in the browser include a lack of direct DOM access, higher potential RAM usage, and the need to run in a WebWorker. Outside the browser, system access may be limited without a WASI runtime.

Orderly is an application built for voice dictation of books and blog posts, using WebAssembly combined with technologies like SQLite in the browser and whisper.cpp for transcription, allowing it to run without a server.

Popular applications using WebAssembly include Figma, CapCut, 1Password, Google Earth, uBlock Origin, and Leachess. These applications leverage Wasm for improved performance and capabilities in the browser.

WASI, or the WebAssembly System Interface, is a standard designed to provide a POSIX-like feature set to WebAssembly, enabling functionalities like file I/O and networking within Wasm code.

Common languages used with WebAssembly include C, C++, Rust, and Go. Other languages with varying support include C#, Zig, Lua, and more.

WebAssembly can be run outside of the browser using tools like Xtism, WasmTime, Spin SDK, and Wasmer, which provide server-side runtimes and environments for executing Wasm code.

Security concerns with WebAssembly include its binary format, which can be obfuscated and harder to analyze than JavaScript, and shared array buffers, which require secure contexts to mitigate vulnerabilities like Spectre Meltdown.

Hosting considerations for WebAssembly applications include the need for proper header configuration to support shared array buffers and OPFS. Services like Render, Netlify, or VPS can be used for hosting models and applications.

Chris Griffing
Chris Griffing
25 min
21 Nov, 2024

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Today's Talk introduces WebAssembly (Wasm) and its versatility in supporting any language on any operating system. Wasm's history dates back to 2011 with Emscripten and NACL, and it has gained importance with the introduction of Wasi, the WebAssembly system interface. Wasm is supported by modern browsers and can be used with languages like C, C++, Rust, and Go. Popular applications like Figma, CapCut, and 1Password utilize Wasm for improved performance. Web Workers and shared array buffers eliminate the need for object serialization. The Talk also discusses the use of Keasley and Drizzle for interacting with SQL. Building and optimizing the application involves considerations of migrations, app readiness, suspense, optimistic UI, and debouncing inputs. Whisper, an open-source AI project, offers models for audio-to-text conversion and is implemented using whisper.cpp. Deployment options include Render, Netlify, Vercel, and Cloudflare, but using a cheap VPS provider with a file server can be a cost-effective alternative.

1. Introduction to Wasm

Short description:

Today I'm going to talk about a different kind of serverless: a case study for SQLite and whisper.cpp running in the browser. I'll explain what Wasm is and how it supports any language on any operating system. Let's dig into it!

Hey everyone, today I'm going to be talking about a different kind of serverless. This is a case study for SQLite and whisper.cpp running purely in the browser. So basically, I built an application. I want to kind of walk you through how I built that application, how it works under the hood a bit, and a little bit of the technology I used.

But also, I'm going to have to explain what Wasm actually is, because that's how we're able to do all of this. So, let's get started. But first, a little bit about me. So I'm a developer advocate at GitKraken. It's my first time in this kind of role, and I'm really loving it. I've been an engineer for about 10 years, and it's quite fun. I'm a Twitch streamer and a lazy YouTuber. As well as a beanie enthusiast. I have a lot of beanies. If you want to find me on other socials, my name is CMGriffin on basically every platform. You can use this QR code to also find me, and you know, yeah, let's connect. Let's chat. But now, let's dig into it.

What is Wasm? So, Wasm is WebAssembly. It's an open standard, and it aims to support any language on any operating system. The idea is, if you can compile from a language like Rust or C++ or Go or whatever, you can run it in your browser as Wasm, and you have this major portability. So, yeah. Wasm. What? Here's a technical or textual representation of what Wasm kind of looks like. And you don't need to memorize this, but let's just imagine this program that is built of these, you know, S expressions. When it's compiled to Wasm, it's going to be turned into this binary format. So, this is that same exact program. Now, you're not going to need to memorize this, but we can go through and kind of look at what some of these things are. So, that first line identifies this as WebAssembly, as a WebAssembly binary, as Wasm stuff, as well as the version of Wasm that we're supporting. There's going to be the module definition. There is the types of the parameters, the number of types that we're passing, you know, the function that we're exporting, you know, the number of results that we'll return, and the type of that return result.

2. Wasm: Function Signature, History, and Wasi

Short description:

There's the function signature, the export section, and the binary format. Wasm's history dates back to 2011 with Emscripten and NACL. In 2015, Wasm was announced, and NACL was deprecated in favor of Wasm. Wasi, the WebAssembly system interface, provides a POSIX-like feature set. Wasm and Wasi's importance is highlighted by the creator of Docker. Running Wasm on the server is the future of computing.

There's the function signature, right? So, that's the arguments that the function takes, the size of it, the number of functions that we're exporting. There's the export section, which is the keyword export, the size of that section, the number of exports. And as we keep going, there's the export section, which is just some more like the name of the export, you know, the function section, which is actually like the code inside of it, the logic of that section and how we're manipulating some of the variables that are passed into it, and then finally the end, which denotes like the end of that binary block. And really, again, you're not going to need to have this memorized, but it's still something interesting, and if you want to read more about it, you can find the binary specification on GitHub.

So now, let's actually consider the history of Wasm. It started back in 2011, which is a long time ago, before I was even really a dev. You could use Emscripten to compile C and C++ all the way back in 2011 to something that would run in the browser. Google had their own version of something Wasm-like called the Native Client, or NACL, and then in 2013, ASM.js was introduced, and ASM.js was JavaScript code that would then be passed to this like ASM.js compiler for the browser to then optimize and run it, and it was very interesting, because you would write JavaScript code, but you would annotate maybe the type of a certain variable to let it know that it would be a float or an integer or things like that. And then in 2015, Wasm itself was first announced, that actual binary format. NACL was then deprecated a couple of years later in favor of Wasm, because Google decided that, yeah, why build something and maintain it themselves when they could actually support an open standard. Broad browser support came around that same time. After that, we got a thing called Wasi, which is the WebAssembly system interface, which I'll go into a little bit more here in a bit. In 2019, Wasm threads were enabled by default in Chrome, and in 2022, Wasm 2.0, a draft for it was created. There's a little bit of contention around that draft and how that works, but we're not going to dig into that too much right now. It may just be worth reading on your own.

So Wasi is the WebAssembly system interface. It's designed by Mozilla, and it provides a POSIX-like feature set, so you can get like file I.O. or networking or things that like your operating system kind of handles for you that when you compile a binary for a certain operating system, those are the bindings that are actually kind of being wired up for you to that binary. So instead, Wasi helps you do that with Wasm code, and when you combine those things together, you get something very powerful. Solomon Hikes, the creator of Docker, once said in 2019, if Wasm and Wasi existed in 2008, we wouldn't have needed to create Docker. That's how important it is. WebAssembly on the server is the future of computing. Just think about that, like really powerful. So the portability that we would get to run Wasm in the Web would also mean we could get that portability at the server level, and you know, we wouldn't need Docker at that point as long as there is a Wasi interface to expose these things for us. And as long as our dependencies could compile to Wasm as well. My favorite talk and example of what this future could look like is The Birth and Death of JavaScript by Gary Bernhardt. Now, the title is misleading. He's not actually anti-JavaScript. He has some very good points about how it's not a perfect language, of course, because there's no such thing. But he goes through and points out that because JavaScript is the way it is, it led to the future that we're seeing where people are pushing for Wasm to get more performant code out of their browser.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Utilising Rust from Vue with WebAssembly
Vue.js London Live 2021Vue.js London Live 2021
8 min
Utilising Rust from Vue with WebAssembly
Top Content
In this Talk, the speaker demonstrates how to use Rust with WebAssembly in a Vue.js project. They explain that WebAssembly is a binary format that allows for high-performance code and less memory usage in the browser. The speaker shows how to build a Rust example using the WasmPack tool and integrate it into a Vue template. They also demonstrate how to call Rust code from a Vue component and deploy the resulting package to npm for easy sharing and consumption.
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
Watch video: Debugging JS
Debugging JavaScript is a crucial skill that is often overlooked in the industry. It is important to understand the problem, reproduce the issue, and identify the root cause. Having a variety of debugging tools and techniques, such as console methods and graphical debuggers, is beneficial. Replay is a time-traveling debugger for JavaScript that allows users to record and inspect bugs. It works with Redux, plain React, and even minified code with the help of source maps.
A Framework for Managing Technical Debt
TechLead Conference 2023TechLead Conference 2023
35 min
A Framework for Managing Technical Debt
Top Content
Today's Talk discusses the importance of managing technical debt through refactoring practices, prioritization, and planning. Successful refactoring requires establishing guidelines, maintaining an inventory, and implementing a process. Celebrating success and ensuring resilience are key to building a strong refactoring culture. Visibility, support, and transparent communication are crucial for addressing technical debt effectively. The team's responsibilities, operating style, and availability should be transparent to product managers.
Making JavaScript on WebAssembly Fast
JSNation Live 2021JSNation Live 2021
29 min
Making JavaScript on WebAssembly Fast
Top Content
WebAssembly enables optimizing JavaScript performance for different environments by deploying the JavaScript engine as a portable WebAssembly module. By making JavaScript on WebAssembly fast, instances can be created for each request, reducing latency and security risks. Initialization and runtime phases can be improved with tools like Wiser and snapshotting, resulting in faster startup times. Optimizing JavaScript performance in WebAssembly can be achieved through techniques like ahead-of-time compilation and inline caching. WebAssembly usage is growing outside the web, offering benefits like isolation and portability. Build sizes and snapshotting in WebAssembly depend on the application, and more information can be found on the Mozilla Hacks website and Bike Reliance site.
Building a Voice-Enabled AI Assistant With Javascript
JSNation 2023JSNation 2023
21 min
Building a Voice-Enabled AI Assistant With Javascript
Top Content
This Talk discusses building a voice-activated AI assistant using web APIs and JavaScript. It covers using the Web Speech API for speech recognition and the speech synthesis API for text to speech. The speaker demonstrates how to communicate with the Open AI API and handle the response. The Talk also explores enabling speech recognition and addressing the user. The speaker concludes by mentioning the possibility of creating a product out of the project and using Tauri for native desktop-like experiences.
A Practical Guide for Migrating to Server Components
React Advanced 2023React Advanced 2023
28 min
A Practical Guide for Migrating to Server Components
Top Content
Watch video: A Practical Guide for Migrating to Server Components
React query version five is live and we'll be discussing the migration process to server components using Next.js and React Query. The process involves planning, preparing, and setting up server components, migrating pages, adding layouts, and moving components to the server. We'll also explore the benefits of server components such as reducing JavaScript shipping, enabling powerful caching, and leveraging the features of the app router. Additionally, we'll cover topics like handling authentication, rendering in server components, and the impact on server load and costs.

Workshops on related topic

Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
How to Solve Real-World Problems with Remix
Remix Conf Europe 2022Remix Conf Europe 2022
195 min
How to Solve Real-World Problems with Remix
Featured Workshop
Michael Carter
Michael Carter
- Errors? How to render and log your server and client errorsa - When to return errors vs throwb - Setup logging service like Sentry, LogRocket, and Bugsnag- Forms? How to validate and handle multi-page formsa - Use zod to validate form data in your actionb - Step through multi-page forms without losing data- Stuck? How to patch bugs or missing features in Remix so you can move ona - Use patch-package to quickly fix your Remix installb - Show tool for managing multiple patches and cherry-pick open PRs- Users? How to handle multi-tenant apps with Prismaa - Determine tenant by host or by userb - Multiple database or single database/multiple schemasc - Ensures tenant data always separate from others
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura
Building a Shopify App with React & Node
React Summit Remote Edition 2021React Summit Remote Edition 2021
87 min
Building a Shopify App with React & Node
Top Content
WorkshopFree
Jennifer Gray
Hanna Chen
2 authors
Shopify merchants have a diverse set of needs, and developers have a unique opportunity to meet those needs building apps. Building an app can be tough work but Shopify has created a set of tools and resources to help you build out a seamless app experience as quickly as possible. Get hands on experience building an embedded Shopify app using the Shopify App CLI, Polaris and Shopify App Bridge.We’ll show you how to create an app that accesses information from a development store and can run in your local environment.
Build a chat room with Appwrite and React
JSNation 2022JSNation 2022
41 min
Build a chat room with Appwrite and React
WorkshopFree
Wess Cope
Wess Cope
API's/Backends are difficult and we need websockets. You will be using VS Code as your editor, Parcel.js, Chakra-ui, React, React Icons, and Appwrite. By the end of this workshop, you will have the knowledge to build a real-time app using Appwrite and zero API development. Follow along and you'll have an awesome chat app to show off!
Hard GraphQL Problems at Shopify
GraphQL Galaxy 2021GraphQL Galaxy 2021
164 min
Hard GraphQL Problems at Shopify
WorkshopFree
Rebecca Friedman
Jonathan Baker
Alex Ackerman
Théo Ben Hassen
 Greg MacWilliam
5 authors
At Shopify scale, we solve some pretty hard problems. In this workshop, five different speakers will outline some of the challenges we’ve faced, and how we’ve overcome them.

Table of contents:
1 - The infamous "N+1" problem: Jonathan Baker - Let's talk about what it is, why it is a problem, and how Shopify handles it at scale across several GraphQL APIs.
2 - Contextualizing GraphQL APIs: Alex Ackerman - How and why we decided to use directives. I’ll share what directives are, which directives are available out of the box, and how to create custom directives.
3 - Faster GraphQL queries for mobile clients: Theo Ben Hassen - As your mobile app grows, so will your GraphQL queries. In this talk, I will go over diverse strategies to make your queries faster and more effective.
4 - Building tomorrow’s product today: Greg MacWilliam - How Shopify adopts future features in today’s code.
5 - Managing large APIs effectively: Rebecca Friedman - We have thousands of developers at Shopify. Let’s take a look at how we’re ensuring the quality and consistency of our GraphQL APIs with so many contributors.