deepstream.io JavaScript Client
raw JSON →The `deepstream.io-client-js` library is the official JavaScript client for connecting to a deepstream.io server, enabling real-time data synchronization, remote procedure calls (RPCs), and publish/subscribe messaging patterns. The current major stable version, `4.0.0`, brought significant advancements over previous versions. This includes a new binary protocol for improved efficiency, full TypeScript declaration files for enhanced developer experience, and a comprehensive shift to promise-based APIs, leveraging `async/await` for asynchronous operations. A key differentiator is its robust offline support, allowing data to be stored client-side using IndexedDB and synchronized upon reconnection. While a precise release cadence isn't explicitly defined, the jump to `v4.0.0` indicates active and substantial development. It's designed for high-performance, real-time applications, offering an opinionated, stateful data synchronization model that distinguishes it from simpler WebSocket libraries or REST-based solutions.
Common errors
error Error: CONNECTION_ERROR - Cannot connect to deepstream ↓
deepstream client factory is initialized with the correct WebSocket endpoint (e.g., ws://localhost:6020 or wss://your.domain:6020). Check firewall rules if connecting remotely. error TypeError: deepstream(...) is not a function ↓
const deepstream = require('deepstream.io-client-js');. For ES Modules, ensure you follow the pattern shown in the documentation: import * as deepstream from 'deepstream.io-client-js'; then use deepstream(...). error TS2307: Cannot find module 'deepstream.io-client-js' or its corresponding type declarations. ↓
deepstream.io-client-js is installed in your node_modules. Review your tsconfig.json for proper compilerOptions and ensure any explicit typeRoots or include paths are correct and point to the bundled type definitions (src/client.d.ts). Modern TypeScript setups often resolve types automatically if the package is installed. Warnings
breaking Version 4.0.0 introduced a new, incompatible binary protocol. Upgrading the client to `v4.0.0` or later requires the deepstream.io server to also be running a compatible version (e.g., `v4.x` or newer) to establish a connection. ↓
breaking With `v4.0.0`, nearly all asynchronous APIs (e.g., `login`, `record.get`, `event.emit`) were refactored to return Promises, aligning with modern JavaScript `async/await` patterns. Callback-based APIs from previous versions are largely deprecated or removed. ↓
gotcha The configuration structure for offline support, including `offlineEnabled`, `saveUpdatesOffline`, and `indexdb` options, was introduced or significantly revised in `v4.0.0`. Older client configurations for offline features will not be compatible. ↓
gotcha The README suggests an explicit `typeRoots` entry in `tsconfig.json` (`"./node_modules/deepstream.io-client.js/src/client.d.ts"`) for TypeScript types. While functional, this approach is less common in modern TypeScript setups, which typically discover types automatically. This explicit path can sometimes conflict with other type resolution strategies. ↓
Install
npm install deepstream.io-client-js yarn add deepstream.io-client-js pnpm add deepstream.io-client-js Imports
- deepstream (factory function)
import * as deepstream from 'deepstream.io-client-js'; - deepstream (factory function, CommonJS) wrong
const { deepstream } = require('deepstream.io-client-js');correctconst deepstream = require('deepstream.io-client-js'); - DeepstreamClient (Type) wrong
import { Client } from 'deepstream.io-client-js';correctimport { DeepstreamClient } from 'deepstream.io-client-js';
Quickstart
import * as deepstream from 'deepstream.io-client-js';
const DEEPSTREAM_URL = process.env.DEEPSTREAM_URL ?? 'ws://localhost:6020'; // Use wss:// for secure connections
const client = deepstream(DEEPSTREAM_URL);
client.on('connectionStateChanged', (connectionState) => {
console.log(`Deepstream connection state: ${connectionState}`);
if (connectionState === 'CLOSED' || connectionState === 'ERROR') {
console.error('Deepstream connection issue. Please check server status and URL.');
}
});
client.login({ username: 'testUser', password: 'password123' })
.then(() => {
console.log('Login successful!');
// Example: Working with a record
const userRecord = client.record.get('users/john-doe');
userRecord.subscribe((data) => {
console.log('User record updated:', data);
});
userRecord.set({ name: 'John Doe', email: 'john.doe@example.com', lastUpdated: Date.now() });
// Example: Publishing an event
client.event.emit('user-activity', { userId: 'john-doe', action: 'loggedIn' });
})
.catch((error) => {
console.error('Login failed:', error.message);
});
// Keep the Node.js process alive for events/records to sync
// Not necessary in a browser environment
setInterval(() => {}, 1000 * 60);