nsqjs icon indicating copy to clipboard operation
nsqjs copied to clipboard

Auto reconnect if writer failed to connect to NSQ

Open fizerkhan opened this issue 9 years ago • 13 comments

Once it is disconnected from NSQ(Manually kill the NSQ server), the 'closed' event has been triggered in the writer. Then start NSQ server, again. But writer failed to reconnect the server. It throws following error

2015-04-20T10:35:58.359Z - error: Error: No active Writer connection to send messages
    at Writer.publish (/Users/guest/myserver/node_modules/nsqjs/lib/writer.js:99:13) 

It will be great if you have auto reconnect in the Writer. Is there any other method to do it?

fizerkhan avatar Apr 20 '15 10:04 fizerkhan

Is there any update about this issue and do you accept a PR for this one? I think this feature would be handy.

mehdivk avatar Apr 21 '15 11:04 mehdivk

I think it's worth addressing since it's a common concern. I'll hopefully get to it soon.

dudleycarr avatar Apr 21 '15 19:04 dudleycarr

Is there any update on this issue? In production, it becomes very critical. Application gets hanged and not responding to any requests. Even in development, writer gets closed if machine went to sleep.

fizerkhan avatar Jun 03 '15 14:06 fizerkhan

I'll work on a solution today. As a workaround, you can listen for the close event and then reconnect. It's not elegant but it will work. That's effectively what the implementation will be within the Reader.

dudleycarr avatar Jun 03 '15 16:06 dudleycarr

Thanks @dudleycarr. Currently i just did worst workaround of process.exit and make supervisord start the node server again. I will try with reconnect and looking for your solution :+1:

fizerkhan avatar Jun 03 '15 16:06 fizerkhan

@dudleycarr is there any update on this issue?

fizerkhan avatar Jul 08 '15 15:07 fizerkhan

Just to add, I also believe it would be a useful addition (to both Reader and Writer). I worked around it manually for now by wrapping the nsqjs library.

hilkeheremans avatar Jan 18 '16 11:01 hilkeheremans

As a workaround it is possible to use https://www.npmjs.com/package/retry to reconnect. It is easy to set listener for closed event and stop messages to be published while reconnecting. The tricky part is to buffer incoming messages during reconnection.

Any chance to have it landed soon? /cc: @dudleycarr

oaleynik avatar Jun 01 '16 19:06 oaleynik

I'm also facing same problems here:

[0] Writer closed
[0] events.js:154
[0]       throw er; // Unhandled 'error' event
[0]       ^
[0] 
[0] Error: read ECONNRESET
[0]     at exports._errnoException (util.js:856:11)
[0]     at TCP.onread (net.js:546:26)

@oaleynik this retry project is real nice, thanks for the tip. Only missing now is buffer the incoming messages as you just mentioned! 👍

thomasmodeneis avatar Jun 16 '16 08:06 thomasmodeneis

@dudleycarr when resolve this issue?

whtiehack avatar Jan 09 '19 03:01 whtiehack

bump

rsaphala avatar Apr 12 '21 10:04 rsaphala

Happens to me too

rbudiharso avatar Dec 20 '22 03:12 rbudiharso

There is a wrapper npm that provide auto-reconnection. https://github.com/Wiredcraft/nsq-strategies#nsqd-connection

xavierchow avatar Dec 20 '22 14:12 xavierchow