Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs reconnect net connection

Tags:

node.js

I have a net client connection to a socket (using net.createConnection(port,host). this works fine.

However, sometimes there is the possibility of the server dropping the connection, and I would like the net connection to automatically reconnect when the server is back up and running

I can trap the on('end') event to catch the initial disconnect, and try to reconnect.

However,how can I check to see if the client has connected, because I want to retry every n milliseconds on a backoff strategy

like image 646
jmls Avatar asked Aug 29 '12 18:08

jmls


People also ask

Does socket IO reconnect after disconnect?

Socket disconnects automatically, reconnects, and disconnects again and form a loop. #918.

What is allowHalfOpen?

What is allowHalfOpen? allowHalfOpen {boolean} Indicates whether half-opened TCP connections are allowed. See net. createServer() and the 'end' event for details. Default: false .

How do I create a socket server listening for connection on given path?

socket.connect(path[, connectListener]) Opens the connection for a given socket. If port and host are given, then the socket will be opened as a TCP socket, if host is omitted, localhost will be assumed. If a path is given, the socket will be opened as a Unix socket to that path.

How do I socket in node JS?

// make a connection with the user from server side io. on('connection', (socket)=>{ console. log('New user connected'); }); Similarly, from the client-side, we need to add a script file and then make a connection to a server through which users send data to a server.


1 Answers

In your end handler, set a timeout that will retry the connection, and set a one time connect handler that will cancel the timeout.

like image 132
ebohlman Avatar answered Sep 21 '22 07:09

ebohlman