Socket.io recovery from disconnections
I have a chat with a server and a client in Socket.io. The client sends and receives messages to / from the server. In order to test a disconnection event, I pull out my Ethernet plug and reattach it after a few seconds.
After that sending messages from the client still works just fine and all previously sent messages during disconnection are resent successfully on both Chrome and Firefox.
For receiving however, it is OK for Chrome but not for Firefox which doesn't receive messages from the server anymore.
The question is what do I do to properly handle such network problems and make my chat robust?
javascript websocket socket.io
add a comment |
I have a chat with a server and a client in Socket.io. The client sends and receives messages to / from the server. In order to test a disconnection event, I pull out my Ethernet plug and reattach it after a few seconds.
After that sending messages from the client still works just fine and all previously sent messages during disconnection are resent successfully on both Chrome and Firefox.
For receiving however, it is OK for Chrome but not for Firefox which doesn't receive messages from the server anymore.
The question is what do I do to properly handle such network problems and make my chat robust?
javascript websocket socket.io
Have you tried reconnecting. For example:socket.on( 'disconnect', function () { console.log( 'disconnected from server' ); window.setTimeout( 'app.connect()', 5000 ); } );
– elegant-user
Nov 27 '18 at 12:38
socket.IO supports ACKs (socket.io/docs/#Sending-and-getting-data-acknowledgements) you can use that to detect if data was sent or not and react respectively. At least this is how I am handling such cases.
– Nika
Nov 30 '18 at 1:04
add a comment |
I have a chat with a server and a client in Socket.io. The client sends and receives messages to / from the server. In order to test a disconnection event, I pull out my Ethernet plug and reattach it after a few seconds.
After that sending messages from the client still works just fine and all previously sent messages during disconnection are resent successfully on both Chrome and Firefox.
For receiving however, it is OK for Chrome but not for Firefox which doesn't receive messages from the server anymore.
The question is what do I do to properly handle such network problems and make my chat robust?
javascript websocket socket.io
I have a chat with a server and a client in Socket.io. The client sends and receives messages to / from the server. In order to test a disconnection event, I pull out my Ethernet plug and reattach it after a few seconds.
After that sending messages from the client still works just fine and all previously sent messages during disconnection are resent successfully on both Chrome and Firefox.
For receiving however, it is OK for Chrome but not for Firefox which doesn't receive messages from the server anymore.
The question is what do I do to properly handle such network problems and make my chat robust?
javascript websocket socket.io
javascript websocket socket.io
edited Dec 1 '18 at 1:16
Balthazar
30.2k106187
30.2k106187
asked Nov 23 '18 at 16:52
Gherman
97911736
97911736
Have you tried reconnecting. For example:socket.on( 'disconnect', function () { console.log( 'disconnected from server' ); window.setTimeout( 'app.connect()', 5000 ); } );
– elegant-user
Nov 27 '18 at 12:38
socket.IO supports ACKs (socket.io/docs/#Sending-and-getting-data-acknowledgements) you can use that to detect if data was sent or not and react respectively. At least this is how I am handling such cases.
– Nika
Nov 30 '18 at 1:04
add a comment |
Have you tried reconnecting. For example:socket.on( 'disconnect', function () { console.log( 'disconnected from server' ); window.setTimeout( 'app.connect()', 5000 ); } );
– elegant-user
Nov 27 '18 at 12:38
socket.IO supports ACKs (socket.io/docs/#Sending-and-getting-data-acknowledgements) you can use that to detect if data was sent or not and react respectively. At least this is how I am handling such cases.
– Nika
Nov 30 '18 at 1:04
Have you tried reconnecting. For example:
socket.on( 'disconnect', function () { console.log( 'disconnected from server' ); window.setTimeout( 'app.connect()', 5000 ); } );
– elegant-user
Nov 27 '18 at 12:38
Have you tried reconnecting. For example:
socket.on( 'disconnect', function () { console.log( 'disconnected from server' ); window.setTimeout( 'app.connect()', 5000 ); } );
– elegant-user
Nov 27 '18 at 12:38
socket.IO supports ACKs (socket.io/docs/#Sending-and-getting-data-acknowledgements) you can use that to detect if data was sent or not and react respectively. At least this is how I am handling such cases.
– Nika
Nov 30 '18 at 1:04
socket.IO supports ACKs (socket.io/docs/#Sending-and-getting-data-acknowledgements) you can use that to detect if data was sent or not and react respectively. At least this is how I am handling such cases.
– Nika
Nov 30 '18 at 1:04
add a comment |
2 Answers
2
active
oldest
votes
As stated in the docs and like you said, the socket should be reconnecting itself normally. Since you can send messages, it seems you are halfway reconnected.
There is a couple events that would be worth for you to add in order to see if nothing is out of the ordinary: reconnecting
, reconnect_attempt
, reconnect
, reconnect_error
and reconnect_failed
.
If nothing comes out of this, what I would advise you to do would be to check if you are getting a disconnect event in your client and recall your connect function, so you get a brand new socket after the network is reestablished:
socket.on('disconnect', () => {
// reconnect
})
From past experiences there was some dirty state stored in the socket object that could explain your issue, and it's worth it to start over with a clean one.
Note for people who do not need the socket-io client, there is also a little library that allows to do just that, reconnect gracefully in case your network drops and should work with a socket-io backend too, reconnecting-websocket.
I catch the following events in this order in both browsers:disconnect
,reconnect_attempt
,reconnecting
,reconnect_error
,reconnect_attempt
,reconnecting
,reconnect
. Seems fine. How do I implement this// reconnect
? Without this the answer is not complete.
– Gherman
Dec 3 '18 at 13:15
And which reconnect is it, from server or client?
– Gherman
Dec 3 '18 at 13:26
At least the advice to listen to these events did help me better understand what is happening
– Gherman
Dec 3 '18 at 13:54
add a comment |
I finally found the root of the problem. Socket.io does reconnect out of the box just fine so you don't need to try to reconnect. However the socket does not seem to be rejoined to the same room after successful reconnection. I managed to solve the problem like this:
socket.on('reconnect', () => {
socket.emit('onEnter', data);
});
Here onEnter
event is used to make the server join me to the room.
However I do not understand why it couldn't retain the room as well.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450451%2fsocket-io-recovery-from-disconnections%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As stated in the docs and like you said, the socket should be reconnecting itself normally. Since you can send messages, it seems you are halfway reconnected.
There is a couple events that would be worth for you to add in order to see if nothing is out of the ordinary: reconnecting
, reconnect_attempt
, reconnect
, reconnect_error
and reconnect_failed
.
If nothing comes out of this, what I would advise you to do would be to check if you are getting a disconnect event in your client and recall your connect function, so you get a brand new socket after the network is reestablished:
socket.on('disconnect', () => {
// reconnect
})
From past experiences there was some dirty state stored in the socket object that could explain your issue, and it's worth it to start over with a clean one.
Note for people who do not need the socket-io client, there is also a little library that allows to do just that, reconnect gracefully in case your network drops and should work with a socket-io backend too, reconnecting-websocket.
I catch the following events in this order in both browsers:disconnect
,reconnect_attempt
,reconnecting
,reconnect_error
,reconnect_attempt
,reconnecting
,reconnect
. Seems fine. How do I implement this// reconnect
? Without this the answer is not complete.
– Gherman
Dec 3 '18 at 13:15
And which reconnect is it, from server or client?
– Gherman
Dec 3 '18 at 13:26
At least the advice to listen to these events did help me better understand what is happening
– Gherman
Dec 3 '18 at 13:54
add a comment |
As stated in the docs and like you said, the socket should be reconnecting itself normally. Since you can send messages, it seems you are halfway reconnected.
There is a couple events that would be worth for you to add in order to see if nothing is out of the ordinary: reconnecting
, reconnect_attempt
, reconnect
, reconnect_error
and reconnect_failed
.
If nothing comes out of this, what I would advise you to do would be to check if you are getting a disconnect event in your client and recall your connect function, so you get a brand new socket after the network is reestablished:
socket.on('disconnect', () => {
// reconnect
})
From past experiences there was some dirty state stored in the socket object that could explain your issue, and it's worth it to start over with a clean one.
Note for people who do not need the socket-io client, there is also a little library that allows to do just that, reconnect gracefully in case your network drops and should work with a socket-io backend too, reconnecting-websocket.
I catch the following events in this order in both browsers:disconnect
,reconnect_attempt
,reconnecting
,reconnect_error
,reconnect_attempt
,reconnecting
,reconnect
. Seems fine. How do I implement this// reconnect
? Without this the answer is not complete.
– Gherman
Dec 3 '18 at 13:15
And which reconnect is it, from server or client?
– Gherman
Dec 3 '18 at 13:26
At least the advice to listen to these events did help me better understand what is happening
– Gherman
Dec 3 '18 at 13:54
add a comment |
As stated in the docs and like you said, the socket should be reconnecting itself normally. Since you can send messages, it seems you are halfway reconnected.
There is a couple events that would be worth for you to add in order to see if nothing is out of the ordinary: reconnecting
, reconnect_attempt
, reconnect
, reconnect_error
and reconnect_failed
.
If nothing comes out of this, what I would advise you to do would be to check if you are getting a disconnect event in your client and recall your connect function, so you get a brand new socket after the network is reestablished:
socket.on('disconnect', () => {
// reconnect
})
From past experiences there was some dirty state stored in the socket object that could explain your issue, and it's worth it to start over with a clean one.
Note for people who do not need the socket-io client, there is also a little library that allows to do just that, reconnect gracefully in case your network drops and should work with a socket-io backend too, reconnecting-websocket.
As stated in the docs and like you said, the socket should be reconnecting itself normally. Since you can send messages, it seems you are halfway reconnected.
There is a couple events that would be worth for you to add in order to see if nothing is out of the ordinary: reconnecting
, reconnect_attempt
, reconnect
, reconnect_error
and reconnect_failed
.
If nothing comes out of this, what I would advise you to do would be to check if you are getting a disconnect event in your client and recall your connect function, so you get a brand new socket after the network is reestablished:
socket.on('disconnect', () => {
// reconnect
})
From past experiences there was some dirty state stored in the socket object that could explain your issue, and it's worth it to start over with a clean one.
Note for people who do not need the socket-io client, there is also a little library that allows to do just that, reconnect gracefully in case your network drops and should work with a socket-io backend too, reconnecting-websocket.
answered Dec 1 '18 at 1:10
Balthazar
30.2k106187
30.2k106187
I catch the following events in this order in both browsers:disconnect
,reconnect_attempt
,reconnecting
,reconnect_error
,reconnect_attempt
,reconnecting
,reconnect
. Seems fine. How do I implement this// reconnect
? Without this the answer is not complete.
– Gherman
Dec 3 '18 at 13:15
And which reconnect is it, from server or client?
– Gherman
Dec 3 '18 at 13:26
At least the advice to listen to these events did help me better understand what is happening
– Gherman
Dec 3 '18 at 13:54
add a comment |
I catch the following events in this order in both browsers:disconnect
,reconnect_attempt
,reconnecting
,reconnect_error
,reconnect_attempt
,reconnecting
,reconnect
. Seems fine. How do I implement this// reconnect
? Without this the answer is not complete.
– Gherman
Dec 3 '18 at 13:15
And which reconnect is it, from server or client?
– Gherman
Dec 3 '18 at 13:26
At least the advice to listen to these events did help me better understand what is happening
– Gherman
Dec 3 '18 at 13:54
I catch the following events in this order in both browsers:
disconnect
, reconnect_attempt
, reconnecting
, reconnect_error
, reconnect_attempt
, reconnecting
, reconnect
. Seems fine. How do I implement this // reconnect
? Without this the answer is not complete.– Gherman
Dec 3 '18 at 13:15
I catch the following events in this order in both browsers:
disconnect
, reconnect_attempt
, reconnecting
, reconnect_error
, reconnect_attempt
, reconnecting
, reconnect
. Seems fine. How do I implement this // reconnect
? Without this the answer is not complete.– Gherman
Dec 3 '18 at 13:15
And which reconnect is it, from server or client?
– Gherman
Dec 3 '18 at 13:26
And which reconnect is it, from server or client?
– Gherman
Dec 3 '18 at 13:26
At least the advice to listen to these events did help me better understand what is happening
– Gherman
Dec 3 '18 at 13:54
At least the advice to listen to these events did help me better understand what is happening
– Gherman
Dec 3 '18 at 13:54
add a comment |
I finally found the root of the problem. Socket.io does reconnect out of the box just fine so you don't need to try to reconnect. However the socket does not seem to be rejoined to the same room after successful reconnection. I managed to solve the problem like this:
socket.on('reconnect', () => {
socket.emit('onEnter', data);
});
Here onEnter
event is used to make the server join me to the room.
However I do not understand why it couldn't retain the room as well.
add a comment |
I finally found the root of the problem. Socket.io does reconnect out of the box just fine so you don't need to try to reconnect. However the socket does not seem to be rejoined to the same room after successful reconnection. I managed to solve the problem like this:
socket.on('reconnect', () => {
socket.emit('onEnter', data);
});
Here onEnter
event is used to make the server join me to the room.
However I do not understand why it couldn't retain the room as well.
add a comment |
I finally found the root of the problem. Socket.io does reconnect out of the box just fine so you don't need to try to reconnect. However the socket does not seem to be rejoined to the same room after successful reconnection. I managed to solve the problem like this:
socket.on('reconnect', () => {
socket.emit('onEnter', data);
});
Here onEnter
event is used to make the server join me to the room.
However I do not understand why it couldn't retain the room as well.
I finally found the root of the problem. Socket.io does reconnect out of the box just fine so you don't need to try to reconnect. However the socket does not seem to be rejoined to the same room after successful reconnection. I managed to solve the problem like this:
socket.on('reconnect', () => {
socket.emit('onEnter', data);
});
Here onEnter
event is used to make the server join me to the room.
However I do not understand why it couldn't retain the room as well.
answered Dec 3 '18 at 13:53
Gherman
97911736
97911736
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450451%2fsocket-io-recovery-from-disconnections%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you tried reconnecting. For example:
socket.on( 'disconnect', function () { console.log( 'disconnected from server' ); window.setTimeout( 'app.connect()', 5000 ); } );
– elegant-user
Nov 27 '18 at 12:38
socket.IO supports ACKs (socket.io/docs/#Sending-and-getting-data-acknowledgements) you can use that to detect if data was sent or not and react respectively. At least this is how I am handling such cases.
– Nika
Nov 30 '18 at 1:04