...

/

Reactive Pattern for Consuming Real-Time Messages

Reactive Pattern for Consuming Real-Time Messages

Learn about the reactive patterns used 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:

webSocket<T>(urlConfigOrSource: string | WebSocketSubjectConfig<T>): WebSocketSubject<T>;

It accepts two types of arguments: either a string representing the URL of our endpoint or a special object of the WebSocketSubjectConfig type that contains the URL of our endpoint, along with other ...