An echo is the fastest way to find out what your application actually knows about a connection. Send a short message, wait for it to come back, and then close the tab or stop the server. The interesting question is what the client can say about the last message when the connection returns.
Look at the open and closed states
Run this small exercise in a local Node.js project after installing the ws package. It binds the listener to the loopback interface and echoes each frame with its original text or binary distinction. The ws project documentation gives the server and event API, including how incoming messages are delivered.
const { WebSocketServer } = require('ws');
const server = new WebSocketServer({ host: '127.0.0.1', port: 8081 });
server.on('connection', socket => {
socket.on('error', console.error);
socket.on('message', (data, isBinary) => {
socket.send(data, { binary: isBinary });
});
});
From a local browser page that allows a loopback connection, open the socket and send only after the open event. The browser exposes connection states through readyState.
const socket = new WebSocket('ws://127.0.0.1:8081');
socket.addEventListener('open', () => socket.send('draft-17'));
socket.addEventListener('message', event => console.log('echo', event.data));
socket.addEventListener('close', () => console.log('closed'));
Give the reconnect a job
Run the exercise twice. On the second run, close the connection just before or after sending. The console can show that the client called send and that the socket subsequently closed. The echo, when received, supplies separate evidence that this particular server handled the frame.
A collaboration tool might attach a client message ID and wait for an application acknowledgement. On reconnect it would need a rule for outstanding messages and for loading the latest document state. A live chart could choose a different rule and fetch one new snapshot.
The useful local test is small. Put opening, echoing and closing into separate rows in the log. Then decide which row the interface should wait for before telling the user their work is saved.
