Channel breakdown detection in js

Hi,

How can I detect in JavaScript when a Phoenix channel/socket breaks down because of network or server problems? Can I set up a callback function for this kind of events? channel.join().on(“error”, xxx) doesn’t do the trick.

1 Like

You might use

socket.onError(() => console.log("there was an error with the connection!"))
socket.onClose(() => console.log("the connection dropped"))

or

channel.onError(() => console.log("there was an error!"))
channel.onClose(() => console.log("the channel has gone away gracefully"))

See https://github.com/phoenixframework/phoenix/blob/master/assets/js/phoenix.js#L68-L95 for more info about different socket and channel hooks.

3 Likes