Triplit in Node.js
Node is the default runtime for the Triplit server. Triplit provides an example implementation (opens in a new tab) and a docker image (opens in a new tab). Read more about self-hosting Triplit in the self-hosting documentation.
Supported storage options
SQLite
SQLite is the default storage option for the Triplit server. It stores all data in a single SQLite database file. This is the recommended storage option for most use cases, as it is fast and easy to set up.
LMDB
LMDB (opens in a new tab) is a fast, memory-mapped database that is designed for high performance.
Example
import { createServer, createTriplitStorageProvider } from '@triplit/server';
const port = +(process.env.PORT || 8080);
const startServer = await createServer({
storage: await createTriplitStorageProvider('sqlite'),
verboseLogs: !!process.env.VERBOSE_LOGS,
jwtSecret: process.env.JWT_SECRET,
projectId: process.env.PROJECT_ID,
externalJwtSecret: process.env.EXTERNAL_JWT_SECRET,
maxPayloadMb: process.env.MAX_BODY_SIZE,
});
const dbServer = startServer(port);
console.log('running on port', port);
process.on('SIGINT', function () {
dbServer.close(() => {
console.log('Shutting down server... ');
process.exit();
});
});
Server side clients
Although the Triplit client is typically used in the browser, it can also be used in Node.js.
Node websockets
A Triplit client relies on the existence of a WebSocket API (opens in a new tab) in the environment it is running in. Since Node.js v21, the WebSocket API is available natively. For this reason we recommend using Node.js v21 or later to run the Triplit client in Node.js.
Node.js v23 or before
Although the websocket API is available in Node.js after v21, it was released with some different behavior (opens in a new tab) from browser implementations. However, you can install a newer version of undici (opens in a new tab) to get a more compliant WebSocket API in Node.js v23 or earlier:
npm install undici@^7
And in your app, you can override the global WebSocket with the one from undici:
import { WebSocket as UWebSocket } from 'undici';
globalThis.WebSocket = UWebSocket;
Node.js v24 or later
You can use the native WebSocket API in Node.js v24 or later without any additional setup.