I was working with socket.io v1.3.6 (nodejs) and tried to emit the data from browser using the below code .
Client Code
var socket = io.connect('http://something.com:3300/');
function sendMessage(message) {
socket.emit('message', message);
}
Server Code
var io = require('socket.io').listen(3300);
io.sockets.on('connection', function (socket) {
messageHandler(socket);
});
function messageHandler(socket) {
socket.on('message', function (data) {
console.log('Captured a message : ' + data);
});
}
my socket server http://something.com:3300/ is down initially , and tried to call few sendMessage() - (say around 10 calls)
As expected in the browser i will get the handshake error in console log.
I waited for 5 mins and started the socket server.
But surprisingly all the messages sent during offline are captured in the server, once the handshake is established.
My questions : 1) Is this offline logic as part of socket.io or WebSocket specification ? 2) I searched a lot of offline mode socket.io questions, and saw some special handling recommendations to capture offline messages. But how this works without those special offline checks ?
Yes, there is an 'offline' buffering for packages to be emitted before the first connect, see here the implementation of socket.js yourself:
https://github.com/socketio/socket.io-client/blob/master/lib/socket.js
(especiall check onconnect and emitBuffered functions/attribs)
I solved this problem with this little change on reconnecting:
mySocket.on('reconnect', function() {
//Avoid that buffered emits getting send again
mySocket.sendBuffer=[];
...
});
By setting the sendBuffer to an empty array you avoid sending the packages again on reconnect. Of course you should then handle the sending attempts on your own.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With