0

I would like to presist the websocket ws client connections on server restart. It seems like the fastest way to do it is using redis. I'm not really sure how I can go about that, since redis doesn't accept objects (i.e. ws clients). How would I store the ws client onto redis?

const wss = new WebSocket.Server({ noServer: true });
server.on('upgrade', function(request, socket, head) {
    console.log(request);

    wss.handleUpgrade(request, socket, head, function done(ws) {
        // Store ws on redis db
        ws.emit('connection', ws, request);
    });
});

P.S. I can not use socket.io. I can only use webSocket

Jessica
  • 9,379
  • 14
  • 65
  • 136
  • 1
    When server restart all connected clients will disconnect. After reboot each connected client will be represented by a new socket. You can identify clients by ID or login/password. – Lemix Jan 19 '20 at 19:54
  • @Lemix How can I re-establish the connection based on id from the server? – Jessica Jan 19 '20 at 19:55
  • The socket-object destroyed when the client disconnects. Client must send an authentication data (ID, token, cookies etc.) after reconnect to server. After received authentication data on server you can associate the socket-object with the real user. – Lemix Jan 19 '20 at 20:16
  • @Lemix So on the client side, I would make a timeout thing that tries reconnecting every 5 seconds or so. After it reconnects to the server, it will start a new socket connection based on the id. Correct? I'm assuming that the actual ws client should be stored in an object in node.js. If that's the case, then what's the point of storing the id's in redis? – Jessica Jan 19 '20 at 20:49

0 Answers0