最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Socket.io emit() delaybuffer flush? - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question asked Sep 7, 2016 at 14:57 Olav KokovkinOlav Kokovkin 1,1801 gold badge13 silver badges22 bronze badges 16
  • 1 Perhaps worth read this article: TCP Performance Gotchas – jfriend00 Commented Sep 7, 2016 at 17:01
  • 1 @jfriend00 nah, let it be. Maybe someone will run into the same issue – Olav Kokovkin Commented Sep 10, 2016 at 11:12
  • 1 @OlavKokovkin how did you solve this? I'm having the same problem. – bitcode Commented Sep 30, 2017 at 4:38
  • 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 Commented Feb 27, 2018 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 Commented Mar 21, 2018 at 7:10
 |  Show 11 more ments

2 Answers 2

Reset to default 4

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

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./feathersjs/feathers/issues/1532

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

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

发布评论

评论列表(0)

  1. 暂无评论