question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

emit during connection lost, automatically re-emit?

See original GitHub issue

Hey there, I am developing a chat app in nodeJS and ReactJS.

Server:

socket.on("new message", data => {
  console.log(data);
})

Client:

const message = {content: "hello"}
socket.emit("new message", message);

However when the client has a connection problem and then gets back online, it sometimes resends the previous emit and sometimes it doesn’t. Can anyone tell me why this is happening randomly?

Or generally asked: When a client drops his connection for a quick time, what happens with the emitted messages during that time? Does it get re-emitted, and if yes, how?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
darrachequesnecommented, Dec 17, 2020

For future readers:

By default, the messages are buffered until reconnection.

There are two possible solutions:

  • check if the socket is connected before sending the packet:
if (socket.connected) {
  socket.emit("new message", "123");
} else {
  // handle the case
}
  • or, use the volatile flag (available in version > 3.0.0). In that case, the packet is discarded if the socket is not connected:
socket.volatile.emit("new message", "123");

Documention here: https://socket.io/docs/v3/emitting-events/#Volatile-events

Regarding the randomness, that sounds weird, I will see if I can reproduce.

1reaction
dinceremrecommented, Dec 17, 2020

thanks for the swift response. can you please explain how does the server behave when it comes to socket.broadcast.to(roomId)? can we attach volatility to broadcast emits?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Socket.io disconnected unexpectedly - Stack Overflow
The first socket was disconnected unexpectedly and another connection is published without front-end's awareness so front-end never send a ...
Read more >
Kotlin's Flow in ViewModels: it's complicated - Christophe Beyls
A graph illustrating the ViewModel Scope in relation to the Activity ... will re-emit the latest value when the flow collection restarts.
Read more >
NRS: CHAPTER 487 - REPAIR, REMOVAL AND DISPOSAL ...
NRS 487.039 Vehicle towed from or immobilized in facility for parking or at ... automatic suspension of license if deposit is reduced or...
Read more >
ACER Guidance
10 of REMIT, trade repositories or competent authorities responsible for overseeing and collecting information on trading in emission allowances ...
Read more >
2021 Instructions for Form 540 Personal Income Tax ...
You may qualify if you have wage income earned in California and/or net ... by the taxpayer for the taxable year to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found