Reactive Pattern for Consuming Real-Time Messages

RxJS has a special type of subject called WebSocketSubject that allows us to communicate with a WebSocket server.

The WebSocketSubject behavior

The WebSocketSubject is nothing but a wrapper around the W3C WebSocket object available in the browser. It allows us to both send and consume data through a WebSocket connection.

The WebSocketSubject creation

In order to use WebSocketSubject, we have to call the webSocket factory function that produces this special type of subject and takes the endpoint of our WebSocket server as input. The following is the function signature:

Get hands-on with 1200+ tech skills courses.