Deepstream Documentation
website·Indexed 34 days ago
https://deepstream.io/docs/A real-time data synchronization and messaging framework. Includes guides for real-time search, WebRTC full-mesh networking, and comprehensive API documentation for Java v2 and JavaScript clients, covering records, lists, RPCs, and event handling.
What's inside Deepstream
- Valve is a permission language used by deepstream to control which users can perform specific actions on data. It allows developers to restrict access for individual users or groups, permission specific actions (e.g., write, publish, listen), and validate incoming data or compare it against stored data. It supports records, events, and RPCs.
Overview of deepstream core capabilities
deepstream is a platform that enables clients and backend services to synchronize data and exchange events with a strong emphasis on security. It allows for server-side configuration without requiring custom code to connect to caches, databases, and message buses. Key features include:
- Records: Real-time synchronization of interactive JSON documents that are persisted and synced across clients.
- Events: A publish-subscribe messaging system where clients subscribe to topics and receive data published by others.
- RPCs: Request-response workflows allowing clients to register functions that can be called by other clients via smart routing.
- Presence: Monitoring of online users and subscriptions to login/logout events.
- Listening: Reactive subscriptions that notify services when a new topic is subscribed to, enabling on-demand real-time data delivery.
- Security: Comprehensive authentication and permissioning from the user level down to individual messages.
Overview of deepstream Remote Procedure Calls (RPC)
Remote Procedure Calls (RPC) in deepstream provide a mechanism for request/response communication, similar to Ajax requests but with built-in load balancing and rerouting. They are useful for tasks such as querying databases (e.g., ElasticSearch), interfacing with external REST APIs, executing multi-step record transactions securely, distributing computational load, or implementing question-answer workflows with users.Overview of deepstream realtime server
deepstream is a standalone, configurable realtime server written primarily in NodeJS. It enables persistent, bidirectional WebSocket connections between clients (browsers, mobile, or backend processes) and the server. Unlike traditional servers, deepstream does not hold custom business logic internally; instead, logic is provided by clients or backend processes that interact with the server. It is suitable for collaboration apps, trading platforms, messengers, multiplayer games, and IoT systems.Overview of Deepstream Listening
Listening is a feature that allows data providers (clients that write data to records, send events, or provide RPCs) to provide data only when other clients express interest by subscribing to a specific pattern. This prevents providers from broadcasting unnecessary data to all clients, reducing bandwidth and third-party data costs. A data provider starts providing data when the first client subscribes and stops when the last client unsubscribes. If multiple providers match a pattern, Deepstream selects one randomly; providers can reject requests (e.g., for load balancing), prompting Deepstream to try another matching provider.Introduction to WebRTC and Signaling Requirements
WebRTC (Web Real-Time Communication) enables point-to-point transmission of text or binary data between peers (typically browsers). While the data transmission is peer-to-peer, a server is required for the initial connection process known as "Interactive Connectivity Establishment" (ICE). This server facilitates the exchange of Session Description Protocol (SDP) signals so peers can locate and connect to each other. In complex network environments, additional STUN or TURN servers may be necessary to bypass NATs.Overview of AnonymousRecord in Deepstream Java Client
AnAnonymousRecordis a record wrapper without a predefined name. It allows a developer to swap the underlying record it is bound to without breaking existing bindings. This is particularly useful for populating user interfaces with data selected from a list of entries.Create a deepstream abstraction service in Angular
To avoid code duplication and maintain a clean architecture, wrap the deepstream client in an Angular@Injectableservice. This allows multiple components to share the same connection instance and provides a centralized place for common operations like logging in or fetching records.import { Injectable } from '@angular/core'; @Injectable() export class DsService { private ds; public dsInstance; constructor() { this.ds = this.dsInstance = new DeepstreamClient('localhost:6020'); } login (credentials?, loginHandler?) { this.ds.login(credentials, loginHandler); } getRecord(name) { return this.ds.record.getRecord(name); } getList(name){ return this.ds.record.getList(name); } }Access user information in Valve permission rules
Theuserobject provides information about the user attempting an action.user.idcontains the username provided at login, anduser.datacontains metadata (such as roles or flags) returned by the authentication webhook or file-based auth. Thenowvariable can be used alongsideuser.datafor time-based validation.record: "*": # Allow access only if 24 hours have passed since signup record: "user.data.timestamp + 24*60*60*1000 < now" write: "user.data.timestamp + 24*60*60*1000 < now"Use the ws-json endpoint for JSON-based communication
Thews-jsonendpoint allows users to interact with Deepstream using JSON serialized text blobs instead of the default Protobuf layer. This is primarily intended for developers building SDKs who wish to avoid the complexity of Protobuf.Handle runtime errors using DeepstreamRuntimeErrorHandler
The
DeepstreamRuntimeErrorHandlerinterface allows developers to handle common runtime errors centrally, reducing the need for repetitive try/catch blocks. It is primarily triggered by asynchronous errors such as timeouts or merge conflicts.Note: Request-specific errors (e.g., an RPC timeout or a record permission error) are not handled here; they are passed directly to the requesting method or caught by a more specific listener.
Connect to Deepstream using the MQTT endpoint
Deepstream provides an MQTT endpoint that supports MQTT authentication (username/password), record retention, and QoS 1 write acknowledgments. Because MQTT uses a single concept for messaging, Deepstream bridges both events and records together. Subscribing to a topic (e.g., 'weather/london') will deliver updates from both
event.emit()andrecord.setData()calls.Key MQTT mapping:
retain: true: Tells Deepstream the message is a record, which will be saved in the database.qos: 1: Tells Deepstream to provide a write acknowledgment confirmation.
const mqtt = require('mqtt') const client = mqtt.connect('mqtt://localhost:1883', { username: 'my-device-name', password: 'my-device-password', properties: { authenticationMethod: 'a-custom-authentication-method', authenticationData: JSON.stringify({ property: "another value" }) } }) client.on('connect', function () { client.subscribe('start/weather/london', (err) => console.log('subscribed')) client.subscribe('stop/weather/london', (err) => console.log('subscribed')) }) client.on('message', function (topic, message) { if (topic === 'start-publishing') { setInterval(() => { client.publish('weather/london', JSON.stringify({ temperature: 12 }), { retain: true, // Saves as a record in the database qos: 1 // Requests write ack }, console.log ) }, 1000) } })