I've a web socket application and using socketIO to send a message to the server to interact with a second page.
Page 1 sends a message to the server, the server tells page 2 to do something. This works well, and because I have many different pages doing this I am using the 'rooms' function of SocketIO:
Page 1
function tryClose(shortCode) { //shortCode is the room name
console.log('Trying to close ' + shortCode)
socket.emit('closePlease', shortCode);
}
========================================================================== Server
socket.on('closePlease', function (data) {
//Send a request to any open sessions with an active browser to close the session
io.sockets.in(data).emit('closePlease');
});
========================================================================== Page 2
socket.on('closePlease', function () {
console.log ('Remote request to close session received!')
//close the modal and display main menu.
socket.emit('leaveRoom', shortCode)
});
My issue starts where the leaveRoom
msg isn't returned to the server if the user has lost connection to the server or the webpage was already closed without firing the 'leaveRoom' msg. This is possible if the network is dropped and the browser closed, and will result in a number of objects and arrays being left open within my NodeJS app. The 'leaveRoom' process is designed to take care of these.
How can I modify the server function so that if the 'closePlease' message is unable to be delivered to the page, the 'leaveRoom' function is executed on the server?
I did look in to callbacks but hit an error message about being unable to use callbacks when broadcasting in a room.
I'm using SocketIO version: "^2.3.0",