WebSockets Reconnection - Mule 4
WebSockets are based on persistent and stateful connections. A connection can abnormally close in the lapse between when it was established and its use in an operation due to factors like idle timeouts, network conditions, and more.
Inbound sockets cannot be automatically reconnected because the remote system must resend the connection request for the connections to be reestablished.
For outbound sockets however, a reconnection policy can be used to re-establish the connection in case it drops.
When your Mule app tries to use a WebSocket that has been abnormally closed, for example to send a message to the socket, reconnection is re-established by replaying the same HTTP request that originally established the outbound socket. After which, the operation is automatically retried.
The following example shows how to send a reconnection policy using a
<websocket:send> operation so that reconnection re-attempts once every second up to 5 times in case the connection is dropped:
<websocket:send socketId="#[vars.socketId]" config-ref="ws"> <reconnect count="5" frequency="1000"/> <websocket:content>Hello World!</websocket:content> </websocket:send>