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 real-time world. 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 following is the API:
export interface WebSocketSubjectConfig<T> { url: string; protocol?: string | Array<string>; openObserver?: NextObserver<Event>; serializer?: (value: T) => WebSocketMessage; deserializer...