Learning the reactive pattern for consuming real-time messages
RxJS has a special type of subject called WebSocketSubject
; this is nothing but a wrapper around the W3C WebSocket object, which is available in the browser. It allows you to communicate with a WebSocket server, both sending and consuming data through a WebSocket connection.
Let’s explore the capabilities of WebSocketSubject
and learn how to use it to consume real-time messages in our project.
Creating and using WebSocketSubject
In order to use WebSocketSubject
, you have to call the webSocket
factory function that produces this special type of subject and takes the endpoint of your 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 of the following:
- A string representing the URL of your WebSocket server endpoint
- A special object...