Learning the reactive pattern for consuming real-time messages
RxJS has a special type of Subject called WebSocketSubject
that allows you to communicate with a WebSocket server. Let's learn about the behavior of this Subject and its capabilities.
The WebSocketSubject behavior
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
, 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 a string representing the URL of your endpoint or a special object of the WebSocketSubjectConfig
type that contains the...