Prevent Duplicate Emits When Using Socket.IO Retries #5434
-
|
I am using the Socket.IO retry functionality with the following configuration: const socket = io({ socket.on("my-event", () => { 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.
Replies: 1 comment 1 reply
-
|
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.
Hi!
To prevent duplicates, you can include an ID when sending your event, for example:
And then on the server:
You can check the step 8 of the tutorial for a complete example: https://socket.io/docs/v4/tutorial/step-8