(smollerGraph)
  • Episode 18
  • 23 min

Graph database in a browser

smollerGraph's library compiled to WebAssembly with Emscripten, unchanged, so the explorer runs in a browser tab on a graph it builds from Wikidata's facts as the page opens, published with the API docs on GitHub Pages.

18 November 2026 · Watch on YouTube

smollerGraph runs in a web browser. Emscripten compiles the library to WebAssembly as it is, as it starts no threads, maps no memory, opens no sockets and keeps its records in fields of fixed widths, and its newer Clang finds five initializers to tidy up. A small C++ file gives JavaScript the questions sg serve answers, as JSON, and the explorer's page asks the graph in the page, which it builds from the Wikidata facts each time it opens. CI builds it in Emscripten's Docker image and publishes it with the API docs, so anyone can try queries, records and similar nodes without installing anything.

Topics: Visualization, Tooling and deployment

The code

Each step on screen is a tag in the database's repository. The code at the end of the episode is ep018/end.

  1. 3:15ep018/step-1WebAssemblyBrowseChanges
  2. 10:43ep018/step-2A newer compilerBrowseChanges
  3. 12:15ep018/step-3In the pageBrowseChanges
  4. 15:54ep018/step-4Where it ranBrowseChanges
  5. 17:20ep018/step-5On GitHub PagesBrowseChanges
  6. 20:01ep018/step-6A decision recordBrowseChanges

Transcript

Intro

0:00 Hi, and welcome to episode eighteen, where we're going to build smollerGraph for a web browser, so that anyone can open a page and run queries on our graph, without installing a thing. Here's a look at where we'll be by the end of it.

0:14 By the end, the explorer we've been building since episode four runs in a browser tab, without a server behind it. The status line says this query ran in the browser, as the whole database is in the page, compiled to WebAssembly. It built its graph of mammals and birds from Wikidata's facts as the page opened, and the table has this query's rows from it, the dog family. The plan comes from our own planner, in the page too, and the canvas draws the answer. The search still finds a node by its name, the inspector still shows the red fox's record, with the bytes it's kept as, and Similar still draws the foxes whose embeddings are most like it.

0:53 There's a website for the series, if you'd like the background on the project. The code is on GitHub, with a tag for each step of each episode, so you can check out any point in a video. And each episode has a page of its own, with its chapters and the code it changed.

Last time

1:08 Last time, we wrote down why smollerGraph is built the way it is. Twenty-two decision records went into the docs, one for each choice that would be costly to undo, with what forced it, what we picked, what else we weighed and what it costs. An accepted record isn't rewritten, and two of them have been superseded by later ones. To try the explorer, though, you still have to clone the repository, build it, load the facts and start sg serve, or run Docker Compose. Today it goes on a web page that anyone can open.

Today

1:41 Here's the plan for today, in three steps. Emscripten, a compiler for C and C++ that targets the browser, is going to build our library as WebAssembly, the binary format browsers run next to JavaScript, along with a small file of ours that lets JavaScript call into it. The explorer's page is going to ask that graph its questions, in the page, where so far it's asked sg serve, over HTTP. And CI is going to publish it on GitHub Pages, next to the API docs, as a few static files, so there's no server to keep running.

Will it compile?

2:15 Before writing anything, I went through what a page can't do that a program on a computer can, to see what would get in our way. Threads in WebAssembly need a page served with headers that GitHub Pages doesn't send, and our library starts no threads of its own. It doesn't map files into memory either, as the pages go through the buffer pool we wrote in episode five. The sockets are all in sg serve's HTTP server, which the browser build can leave out. Files are plain opens, reads and writes, which Emscripten gives us from a file system it keeps in memory, so the graph's files are there while the page is open and gone when it closes. And WebAssembly's pointers are thirty-two bits, where the Mac's are sixty-four, which would change any record that held a pointer or a size t. Ours are fixed-width numbers and characters, and a static assert checks each record's size, so a node's record is sixty-four bytes in both, or the build fails. So my guess was that it compiles as it is, and it did, with a few warnings we'll come to.

WebAssembly

3:16 In the CMake file, after the library, a new block runs when EMSCRIPTEN is set, which it is when CMake runs through emcmake, Emscripten's wrapper that hands CMake its compiler. The comment over it says what's in it: the library as WebAssembly, and the page's way into it, sg dot js, with the Wikidata facts it builds its graph from. A browser runs no server, and our tests are GoogleTest's, so neither is built here, and the module gets a test of its own, run in Node, as a page would load it. Inside the block, sg web is an executable made from a single new file, web, which we're going to write next, and its output name is sg, so the build writes sg dot js and sg dot wasm. It links the library, as sg and the tests do, without a line of it changed. Its link options start with dash l embind, which links embind, Emscripten's way of calling C++ functions from JavaScript by name. Modularize wraps the output in a function that loads the module when the page calls it, and export ES6 makes that a JavaScript module, which a page can import. Allow memory growth lets the module's memory grow past what it starts with, as the embeddings alone take over twenty megabytes. Preload file packs the Wikidata facts into a file called sg dot data, which Emscripten unpacks into its file system, at slash wikidata dot txt, before our code runs. SHELL in front makes CMake pass the flag and its path as two arguments, the way a shell would split them. Then a test, called web, which runs a JavaScript file in Node, given the module's path by a generator expression. Return ends the CMake file there, so the browser build skips sg, with its HTTP server, and the GoogleTest tests, as the comment said. The new file, web, is the page's way into the library. Its first include is embind's header file, and the comment at the top of the file says how it works: the graph runs in the page, as WebAssembly, and answers the explorer's questions as sg serve answers them over HTTP. Its files live in Emscripten's file system, in memory, so the graph is built from the facts each time the page opens, and it's gone when the page closes. Then chrono, the standard library's header for time, to time the build, fstream to read the facts file, string and vector. From Abseil come status, status or and string concatenation, which the rest of the code uses for errors and for building strings. Then four headers of our own. The API's header is where sg serve's answers are written as JSON, the embeddings' header has FastRP, the facts' header loads facts, and the graph's header has the graph. Inside our namespace, the functions go in an unnamed namespace, so nothing outside this file can call them by mistake. The graph function returns the page's one graph, made the first time it's called, behind a pointer that nothing deletes. Google's style guide doesn't allow a static object with a destructor, even one inside a function, and the comment over it says why we don't need one: the page just closes. The answer function takes a status or, which holds either a JSON string or an error, and turns it into what sg serve sends: the JSON, or the error's message as JSON, as its comment says. The build function is new, and it takes three strings: the graph's folder, the facts file, and the relationship type for the embeddings. From JavaScript, they're plain JavaScript strings, which embind turns into standard strings on the way in, and the string we return goes back to JavaScript as one of its own. It starts a clock, gets the graph, and opens it in the folder it's given. The if starts with a declaration, the status, so the status lives inside the if and nowhere else, and when it isn't OK, build answers with its message, as JSON. Then it opens the facts file with an input file stream, and loads the facts with the function sg load uses. FastRP gives each node an embedding, with sg embed's seed of forty-two, and a loop stores each one in the graph. Flush writes the changed pages out of the buffer pool, into the files in memory. It answers with string concatenation, as JSON: the counts of nodes and relationships, and how long it all took, in milliseconds. Its comment sums it up, in that order, from the folder and the facts to the size of the graph and the milliseconds, so a reader knows what comes back without following the code. The names function is a single line, calling names JSON, the function sg serve's handler calls, so the page and the server can't disagree. Query goes through answer, as a query can fail to parse, and so do node and similar, which fail for an id the graph doesn't have. Names can't fail, so its JSON comes back as it is. Then the two namespaces close, the unnamed one and ours, smollergraph. Last, EMSCRIPTEN BINDINGS gives each of the five a name that JavaScript can call it by, as a function on the module. The block runs as the module starts, before the page calls anything, and registers each function with its argument types. So a call from JavaScript with the wrong number of arguments throws an error in the page, without reaching our code. The module gets a test of its own, written in JavaScript, as Node can load what a page loads. It imports assert, Node's module of checks that throw when they fail. The comment at the top says what the test is, the module loaded in Node as a page loads it. It also says where the loader reads sg dot data from: the folder it runs in, which for ctest is the build folder. Then pathToFileURL, as the module's path comes in as an argument, and import wants a URL. It imports the module, and load, its default export, starts it. Build makes the graph, and the first check is that there are over twenty thousand nodes, so the facts came along in sg dot data. Then a query for the red fox's genus, whose row has to be Vulpes, which tries the parser, the planner and the records in one go. The names that start with "Red f" have to be the red forest duiker and the red fox, from the name index. Then the genus's record has to be named Vulpes, and Similar has to find ten nodes like it, from the embeddings that build made. A query it can't parse has to come back as an error, with the parser's message about the missing bracket, the way sg serve sends one, and the comment over it says so. The last line prints how big the graph is, and how long it took to build, which we're going to read in a moment. And the build web folder goes in the git ignore file, next to build.

9:51 Emcmake runs CMake for us, with Emscripten's toolchain file, which tells it to use em c c and em plus plus in place of the Mac's compilers. Then Ninja builds it all, Abseil first, compiled to WebAssembly from its source, then our library and the new file, web. There's no error, but there are warnings, and grep keeps the ones about our own files. Abseil prints a few dozen of its own, about version macros Emscripten has renamed, which are Abseil's to fix. Ours are five designated initializers, in query and plan, that leave a field out. Emscripten's Clang is newer than the Mac's, and it warns about those with dash W extra, where the Mac's doesn't. And the lib folder has GoogleTest's libraries, built for WebAssembly, which nothing in this build uses.

A newer compiler

10:43 A field we leave out of a designated initializer gets its empty value, as C++20 says, and each of these wanted it empty. So nothing was wrong, though I'd still fix them, as a build that prints five warnings each time teaches us to skim past them, and then a new one gets missed. In query, both sides of the search for a cheapest path start with an empty queue, which they now say. In plan, a returned path's column has no key, a returned property's column has no function, and a property's cell has no path, and each initializer names its empty field. In the CMake file, GoogleTest's fetch content gets exclude from all, as Abseil's has, so a build compiles it for a target that needs it, the tests, and not for the browser.

11:27 Built again from scratch, grep finds no warnings about our files, so the count is zero. And the lib folder is empty this time, so GoogleTest stayed out of it. The build wrote three files, and sg dot wasm is the whole database, about three hundred kilobytes, sg dot js is the JavaScript that loads it, and sg dot data holds the facts, about three megabytes. Ctest runs the test in Node, and it passes. Run on its own, from the build folder, it prints what it built: about twenty-two thousand nodes and as many relationships, with their embeddings, in about a fifth of a second. I'd expected WebAssembly to be a lot slower at it, and it's about as fast as the program on this Mac, which takes about a fifth of a second too.

In the page

12:16 Now the page, and in the CMake file, the module's files go to a folder of their own, explorer, inside the build folder, through runtime output directory. Then the page's files, a new module called api among them, each with the UI folder's path in front, and the comment over them says the benchmark's page stays out, as it needs sg serve. After the module is built, a command copies them into the explorer folder, so that folder is the whole explorer, which any static file server can serve. The test needs a working directory now, as Emscripten's loader looks for sg dot data in the folder it runs in, which was the build folder. The module has moved, so the test runs in the module's folder, and the comment says why. The api module is where the page asks the graph its questions, and its comment at the top says it's either sg serve, over HTTP, or the graph running in the page, and both answer with one kind of JSON. A get function fetches a URL and reads its JSON. An object called over HTTP has the four questions, each one fetching the URL the page used to fetch. Query and names put their text in the URL's query string, and node and similar put the node's id in the path, as sg serve's routes expect. The in-page function tries to import sg dot js from beside the page. In the browser build it's there, and under sg serve it isn't, so the import fails, and the function returns null, as the comment over it explains. When it's there, the module loads, and build makes the graph from the facts, along the parent taxon relationships, as sg embed does in Docker Compose. It returns the four questions too, each parsing what the module answers. Last, api is whichever of the two we've got. The await is at the top level of the module, which modules allow and plain scripts don't, so whatever imports api waits for the graph. In app, the comment at the top changes first. The query is run by sg serve or by the graph in the page, through the api module, with its answer drawn as a graph, and shown as a table and as a plan. Then the import of api, next to the import of the view. Importing it means none of app's code runs until the graph is built, as we saw a moment ago. Run used to build a fetch for the query, and read its JSON. Now it asks api dot query, once for the answer and, unless the query is an explain already, once more for its plan, and waits for both at once. The comment over run says why there are two: the plan tab always has the plan of the query that's shown. The search asks api dot names for the names that start with what's been typed, whenever there's something in the box. Expanding a node asks api dot node for its record, to draw its neighbours. Similar asks api dot similar for the nodes most like the one selected, and the inspector asks api dot node for the record it shows. The rest of the page stays as it was, as both ways answer with the JSON it already reads. The test lost its comment about where sg dot data is read, which moved into the CMake file.

15:27 To try it, we build with emcmake and serve the explorer folder with a plain static file server, such as Python's. The page opens on its first query, the species of the dog family and their genera, drawn on the canvas, without a server to ask. The status line is wrong, though, as it still says the query ran on the server, and in this page there isn't one. I wrote that line in episode ten, when the graph was on the server and nowhere else.

Where it ran

15:54 So the api module says where its graph is. Each of the two objects gets a field called where, on the server for HTTP, and in the browser for the graph in the page, and the comment at the top says what it's for. In app, the status line ends with api dot where, in place of the words on the server, and the comment over the summary says where the graph is, on the server or in the browser. When I rebuilt and reloaded the page, though, it still said on the server. The explorer folder still had the old copy of app, as the command that copies the page's files runs after the module is linked, and changing app doesn't link anything again. So the copy after the build goes, and in its place, configure file copies each of the page's files at configure time. CMake keeps a note of them, so changing one runs the configure step again, and copies it again. The comment says why it's this way: a step after the build runs when sg dot wasm is linked again, and at no other time.

16:55 Rebuilt, the status line says the query ran in the browser, in about half a millisecond. It read no pages from the graph's files, as building the graph left them all in the buffer pool, in the page's memory. The plan tab shows the plan the page's own planner made, the operators from episode seven, and the table has the rows, a species, its genus and the family, for each of the thirty-nine.

On GitHub Pages

17:20 To publish it, CI has to build it, so the Dockerfile builds it first. The new stage, explorer, starts from Emscripten's own image, at seven point oh point oh, the version on this Mac. Its comment says what it is: the explorer as it runs in a browser, the library built as WebAssembly in Emscripten's image, beside the page's files, with the module's test run in Node. It installs Ninja from Ubuntu's packages, which the image doesn't have, and clears apt's lists, as our other stages do. Then it copies the source in, configures it with emcmake, builds it, and runs ctest, so the module's test runs in the Node that comes with the image. Then the docs stage, which holds the pages CI publishes, copies the explorer folder in too, at slash explorer, and the comment over it says so: the docs and the explorer, for GitHub Pages to serve. The Docker ignore file gets the build web folder, so a build doesn't send the Mac's WebAssembly along with the source. In the CI workflow, the step that builds the docs is now called build the API docs and the explorer. The comment over it says the docs, with the explorer, go up from the public repository, where each episode arrives once it's out, and the private one, which spends Actions minutes, doesn't build them. Further down, the comment over the job that deploys them gives the explorer's address, at slash explorer, next to the docs. The README gets a section of its own, in a browser, after the one on the UI. It says the UI runs in a browser too, without a server, and that built with Emscripten, which Homebrew installs, the library is WebAssembly. The page builds the graph of mammals and birds, with their embeddings, from the Wikidata facts each time it opens, in its own memory. Then the four commands: emcmake to configure the build web folder, the build, ctest for the module's test, and Python's file server on the explorer folder, on port eighty-four seventy-five, one past sg serve's. Then the address to open, and where CI publishes it, at nenuadrian dot github dot io, slash smollerGraph, slash explorer. And further down, the Docker section says the docs stage writes the explorer too, in a folder of its own.

19:32 Docker builds the stages the docs need, and their images and package installs come from its cache, from the builds before. So what runs is Doxygen, as in episode sixteen, and the explorer's build in Emscripten's image, side by side, in about fourteen seconds. The module's test ran inside the explorer stage, and a failure would have stopped the build there. And the site folder has the explorer, the page's five files, and the three that the build made.

A decision record

20:01 Choosing to run the explorer in a browser is the kind of choice the records are for, so it gets record twenty-three. The context is what it took to try the explorer until today, and why the library compiles as it is, without threads, mapped memory or sockets, with records of fixed widths. The decision names the files, the web file, the api module and the explorer stage, and that the page builds its graph each time it opens and keeps nothing. There are three alternatives, and the first is a server for everyone, behind Caddy, like episode eight's, which is a machine to pay for and keep up, for a graph nobody can change. Shipping the graph's folder is about twenty-nine megabytes to download, where the facts are three. And keeping the graph between visits, in the browser's IndexedDB, wasn't weighed, as our Cypher can't change a graph, so there's nothing to keep, and the record says so. The costs are that the page builds the graph each time, before its first answer, and that it's the one graph, Wikidata's, so a graph of your own still needs sg. And the index lists it, so the next record is number twenty-four.

What's missing

21:09 Here's where the page stops, for now. It has one graph, built from the facts it was shipped with, where a page could just as well load a facts file you pick from your own disk. Nothing is kept between visits, which is fine while a query can't change the graph, and would need IndexedDB, the browser's own database, once it can. And it runs on the page's main thread, so a slow query holds up the page while it runs. A web worker would move it off the page's thread, without any headers. Splitting a query over several threads would need the page served with two headers, for what's called cross-origin isolation, which GitHub Pages doesn't let us set.

Wrap-up

21:47 So, the explorer runs in a browser now. Emscripten compiled the library to WebAssembly, along with a small file that gives JavaScript the questions sg serve answers, and the page asks whichever graph it has, the one in the page or the server. CI builds it in Emscripten's image and publishes it with the API docs. What I like most is that the library compiled as it was, and all it needed were five initializers that a newer compiler warned about. That comes down to choices made in episodes one, two and five: an embedded library, records of fixed widths, and a buffer pool of our own.

22:23 I'd love to hear what you'd try first on the page, and whether you've taken C++ of your own to a browser, and how it went. Let me know in the comments, and if you'd like to follow along, subscribe to the channel. I hope you find this useful, and thanks for watching.