RESTHeart WebChat

This example uses Angular 12 and RESTHeart's WebSockets with Change Streams to provide an instant, zero lines of code API for a realtime chat application. Just insert a nickname below to start playing with the demo:

Note: the above app is a demo in constant development, so the implementation can change daily. I's included here via an <iframe> tag, but the full Web application is available at https://chat.restheart.org.

The complete source code of this Angular Web application is available on GitHub.

How it works?

In short, RESTHeart leverages MongoDB's Change Streams to instantly notify database modifications to clients (Web or Mobile apps), through the WebSocket protocol.

RESTHeart is an open-source runtime and a low-code SDK for Microservices with declarative security and instant Data APIs on MongoDB or any compatible database (e.g. Percona Server for MongoDB, AWS DocumentDB and Azure Cosmos DB).

It immediately exposes all of MongoDB’s capabilities through a comprehensive set of REST, GraphQL and WebSocket APIs. RESTHeart embeds Undertow, a flexible performant web server written in Java, providing both blocking and non-blocking API’s based on NIO.

architecture Developers can save at least 80% of backend code to manage security and serve content to Mobile Apps and Angular, React, Vue or other SPA frameworks.

Ingredients

The WebSocket API (WebSockets)

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.

Websocket_connection

MongoDB Change Streams

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 WebSockets

RESTHeart embeds a WebSocket server implementation that allows to expose MongoDB's Change Streams to Web browsers and any kind of HTTP/WebSocket client (for example, Postman offers this feature).

With RESTHeart is possibile to create Web or Mobile apps that can be asynchronously notified in real time of data changes, avoiding unnecessary polling. Because change streams use the MongoDB's Aggregation Framework, applications can also filter for specific changes.

References