Prevent Duplicate Emits When Using Socket.IO Retries #5434
Unanswered
ayyanar-finstein
asked this question in
General
Replies: 1 comment
-
|
Hi! To prevent duplicates, you can include an ID when sending your event, for example: function randomId() {
return Math.random().toString(36).substring(2, 11);
}
socket.emit("my-event", randomId());And then on the server: socket.on("my-event", (offset) => {
// store the offset, either on the socket object itself or in a database
});You can check the step 8 of the tutorial for a complete example: https://socket.io/docs/v4/tutorial/step-8 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am using the Socket.IO retry functionality with the following configuration:
const socket = io({
retries: 3,
ackTimeout: 15000
});
When the server acknowledgment is not received within the timeout, the event is retried.
Inside the event listener, I emit another event:
socket.on("my-event", () => {
socket.emit("another-event", params);
});
Due to retries, this listener executes multiple times, causing another-event to be emitted more than once.
My requirement is that another-event should not be emitted multiple times for the same retry attempt.
Could you please suggest the best way to avoid duplicate emits in this case?
Beta Was this translation helpful? Give feedback.
All reactions