To try and reconnect to the socket server from the client side if the server is not already running?
I have a chat app using Socket.io and AngularJS 1.x. I used https://github.com/btford/angular-socket-io for Socket. My factory / service is like below.
var serverBaseUrl = 'http://localhost:4000';
app.factory('socket', function (socketFactory) {
try {
var myIoSocket = io.connect(serverBaseUrl, {
'reconnection delay': 1000,
'reconnection limit': 1000,
'force new connection': true
});
var socket = socketFactory({
ioSocket: myIoSocket
});
return socket;
} catch (e) {
console.log('System : Socket server is out of service. We will look into this and bring it back up.');
return null;
}
});
I can reconnect to the server only for the case of loading the client page and at that point the server is up and running , my factory has a socket value, if I stop the server and restart the server, the client will be able to connect to the server. I achieved this through the methods below.
socket.on('reconnect', function () {
console.log('System : Reconnected to the server');
socket.emit("abc", xyz);
});
socket.on('reconnecting', function () {
console.log('System : Attempting to re-connect to the server');
});
The above scenario is fine, but I'm talking about a scenario where my app is loaded for the first time and at that point my server is down or the nodejs app is stopped. ... For this scenario, the factory is executed only once, when the application is loaded in the browser. Let's say that after a few minutes, the server is up and running, but the client won't connect to the server unless the user refreshes the page. But I don't want that, the client should be able to connect to the server without updating. The client should constantly try to connect to the server even if the server / service is down.
How can I achieve this?
source to share
No one has answered this question yet
Check out similar questions: