Skip to content

Reconnecting Websocket

Io.reconnectingWebsocket connects and tries to keep a connection to a WebSocket server.

This is handy because you lose connection if you close the lid of your laptop, lose wifi etc. Instead of having to listen for and handle the state of the raw WebSocket, this function does it for you.

The usage is simple:

import { reconnectingWebsocket } from 'https://unpkg.com/ixfx/dist/io.js'
const ws = reconnectingWebsocket(`wss://somehost.com/ws`, {
onMessage: (msg) => {
// Do something with received message...
}
}
// Send some data
ws.send(JSON.stringify(someData));
// Check state of connection
ws.isConnected(); // true/false

See the API documentation for more examples and info.