Learning the reactive pattern for handling reconnection
When the connection to the WebSocket server is lost, the channel will be closed, and WebSocketSubjet
will no longer emit values. This is not the expected behavior in the world of real time. The reconnection capability is a must in most cases.
Therefore, let’s imagine, for example, that after a disconnection, a system tries to reconnect after every 3 seconds. The solution, in this case, is intercepting the closure of the socket and retrying the connection. How can we intercept the closure of the connection?
This is possible thanks to WebSocketSubjectConfig
, which is responsible for customizing some behavior in the socket life cycle. The WebSocketSubjectConfig
interface in RxJS provides several properties that you can use to configure a WebSocketSubject. These properties allow you to customize various aspects of WebSocket communication:
export interface WebSocketSubjectConfig<T> { url: string; ...