Reactive Pattern for Handling Reconnection

Learn about the reactive pattern used for handling reconnection.

Reconnection

When the connection to the WebSocket server is lost, the channel will be closed, and WebSocketSubject will no longer emit values. This isn’t 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 three 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 in recipes-book-front/src/app/core/services/real-time.service.ts:

Get hands-on with 1400+ tech skills courses.