Looking for Cloud Services or Professional Support? Check restheart.com

Edit Page

WebSocket API Overview


This section describes the WebSocket API that allows publishing data events to WebSocket clients.

The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user’s browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

MongoDB’s Change streams allow applications to access real-time data changes without the complexity and risk of tailing the oplog. Applications can use change streams to subscribe to all data changes on a single collection, a database, or an entire deployment, and immediately react to them. Because change streams use the aggregation framework, applications can also filter for specific changes or transform the notifications at will.

RESTHeart Change Stream

RESTHeart embeds a WebSocket server implementation that allows to expose MongoDB’s Change Streams to Web browsers and any kind of HTTP/WebSocket client.

With RESTHeart is possible to create Web or Mobile apps that can be asynchronously notified in real time of data changes. Because change streams use the aggregation framework, applications can also filter for specific changes.

For example, if the stream all is defined on the collection messages, clients can connect via WebSocket to ws://mydomain.com/messages/_streams/all and receive real time notification of data changes occurring in the collection.

Exposing a WebSocket Server, clients may be promptly notified about these changes only if necessary, avoiding network expensive common practices like polling.

Change streams require at least MongoDB configured as a Replica Set
When the stream collection metadata is modified or the collection or the db is deleted, all related WebSocket connections are closed and the change streams are consequently updated.