3

I'm using socket.io to send messages to browser. On node.js side I'm doing

socket.emit('message', data);

On browser-side I'm doing

socket.on('message', handleData);

Now this works fine. For testing purposes I'm manually triggering (from node-inspector console) the socket.emit(). I'm able to do this 2-3 times after which the next message takes a long time to deliver. About 10 seconds.

My messages are rather short. Compression is enabled and the object JSON {"could be about": "this long"}. When testing with longer strings, all messages are sent instantly. So this has something to do with buffering/optimization, but in our case, it's important that all messages are sent instantly.

Does anyone have any insight into this delay? Thanks

Olav Kokovkin
  • 1,150
  • 1
  • 13
  • 21
  • Have you looked into long-polling? I remember reading a blog about how socket.io degrades to using it. From a google search I turnd up this - http://stackoverflow.com/questions/8350630/nodejs-with-socket-io-delay-emitting-data not sure if it's any help. – N.J.Dawson Sep 07 '16 at 15:01
  • Can you reproduce this issue in a set of code you can post here using programmatic data sends (not typing at the console) so it's entirely reproducible. On the surface of things, `.emit()` just ends up doing a TCP send which due to the Nagle algorithm may buffer small packets for a very short period of time (waiting to see if there is some other data about to be written that can be bundled with it). But, 10 seconds would not likely be caused by the Nagle algorithm. – jfriend00 Sep 07 '16 at 16:49
  • You can turn off the Nagle delays with `socket.setNoDelay(true)` as shown in the [doc here](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay). – jfriend00 Sep 07 '16 at 16:50
  • FYI, the reason I doubt this is related to the Nagle delay is that the Nagle delay on Linux is only 40ms. – jfriend00 Sep 07 '16 at 17:00
  • 1
    Perhaps worth read this article: [TCP Performance Gotchas](http://www.evanjones.ca/tcp-performance.html) – jfriend00 Sep 07 '16 at 17:01
  • @jfriend00 no, socket.io doesn't use node's net.Socket (http://stackoverflow.com/a/32192006/961064) I'll try to write a test setup at a later date when this problem becomes more pressing. Right now don't have the time – Olav Kokovkin Sep 08 '16 at 07:40
  • @OlavKokovkin - Per that info, socket.io already turns off the Nagle delay so that wouldn't be your issue. I did read another post somewhere that described a situation similar to yours and the problem turned out to be some sort of local security/anti-virus settings that would occasionally interfere with network traffic. So, that's another possibility. – jfriend00 Sep 08 '16 at 20:37
  • @jfriend00 I agree. This seems to be a more general network stack issue than something in socket.io implementation. Closing – Olav Kokovkin Sep 09 '16 at 11:02
  • I'm voting to close this question as off-topic because the described problem was not directly related to socket.io – Olav Kokovkin Sep 09 '16 at 11:04
  • @OlavKokovkin - You can just delete your own question if you want to. – jfriend00 Sep 09 '16 at 15:19
  • 1
    @jfriend00 nah, let it be. Maybe someone will run into the same issue – Olav Kokovkin Sep 10 '16 at 11:12
  • 1
    @OlavKokovkin how did you solve this? I'm having the same problem. – bitcode Sep 30 '17 at 04:38
  • @bitcode I'm not sure if I remember correctly, but I think there was no actual problem. Try using different test strings. Or maybe we agreed that it wasn't a problem for us in production scenario – Olav Kokovkin Oct 01 '17 at 10:36
  • 1
    I have the exact same issue. Some times I press send and it is send in no time... some times I press send and it takes up to 20sec OR I just press send again(!) and it appears imediatly... which seems to be a bug to me too?! – Informatic0re Feb 27 '18 at 23:18
  • 1
    @Informatic0re did you get it to work? I experience the same issue, tried the getting started chat and sometimes a message takes 1ms other 10 seconds. – Todilo Mar 21 '18 at 07:10
  • @Informatic0re maybe you have the same issue I have. I am running on socket io 2.0.4 which seem to have an issue with this. Despite upgrading client to correct version the delay did not work. Luckily someone found a solution for it: https://github.com/socketio/chat-example/issues/13 For completeness write this: const io = require('socket.io')(httpServer, { wsEngine: 'ws' }); The wsEngine is the important part. – Todilo Mar 21 '18 at 07:18

2 Answers2

3

I had the same problem. Although it's been years, these tips would be useful to someone else witht eh problem.

  1. How To Cancel, Timeout, or Clear Queued Messages In Socket.IO

  2. https://github.com/feathersjs/feathers/issues/1532

    socket.on('connect', function() { socket.sendBuffer = []; // do stuff });

    socket.on('reconnect', function() { socket.sendBuffer = []; });

sandeepani
  • 353
  • 3
  • 12
3

A link to the official documentation (v4.0):

https://socket.io/docs/v4/client-offline-behavior/

Indeed there are three ways to fight buffering on client side:

  • use the connected attribute of the Socket instance
if (socket.connected) {
  socket.emit( /* ... */ );
} else {
  // ...
}
  • use volatile events
socket.volatile.emit( /* ... */ );
  • empty the internal buffer upon reconnection
socket.on("connect", () => {
  socket.sendBuffer = [];
});

Docs on volatile emits:

https://socket.io/docs/v4/emitting-events/#Volatile-events

shitpoet
  • 419
  • 5
  • 11