slack-bot-api icon indicating copy to clipboard operation
slack-bot-api copied to clipboard

Reconnect not working

Open diego1686 opened this issue 9 years ago • 14 comments

Hi! I added a listener for the 'close' event in order to handle disconnections. I'm seeing the message in the logs but the bot is offline now. What I'm doing wrong? Thanks!

// handle disconnections
bot.on('close', function(data){
    console.log("Connection closed... Reconnecting.")
    bot.login();
});

diego1686 avatar Feb 16 '16 14:02 diego1686

I have the same doubts

robsonbittencourt avatar Jun 27 '16 14:06 robsonbittencourt

same issues how to handle reconnects?

puzanov avatar Jul 18 '16 11:07 puzanov

Having the same issue. Does the above not work?

nkramaric avatar Aug 26 '16 14:08 nkramaric

Same here

MattCBelanger avatar Aug 26 '16 15:08 MattCBelanger

Thanks for the issue. What data is coming with a close event? Because when I try to emulate it and close a websocket manually, login() works fine.

mishk0 avatar Dec 22 '16 17:12 mishk0

Shouldn't this be a part of the library itself?

var bot = new Slackbot({});

bot.on('message', function(ev) {
    if (ev.type == 'reconnect_url') {
        bot.wsUrl = ev.url;
        return;
    }
    // ... 
});

bot.on('close', function() {
    bot.connect();
});

WoLfulus avatar Mar 23 '17 05:03 WoLfulus

I'm seeing the close event in my logs after which my bot goes offline. How can I always keep the bot online? What is the way to properly restart the bot after the close event?

alexeystrakh avatar Jul 01 '19 04:07 alexeystrakh

Has someone managed to fix this issue?

KoichaDev avatar Oct 21 '19 13:10 KoichaDev

Same question. How to keep bot always connected?

dmnBrest avatar Oct 24 '19 10:10 dmnBrest

I have absolutely no idea, but the only solution I can think of is deploying the bot on a ubuntu server and making sure the crontab will run a bash script to turn off and on the bot every hour.

This is not a root solution, but rather work around it (not the most ideal way to do it).

I seriously hope someone could try and find a solution on how to fix this.

KoichaDev avatar Oct 24 '19 10:10 KoichaDev

I've noticed this solution bot.on('close', function(data) { console.log(data) bot.connect(); }); fails with infinitive loop and 1006 error in response.

dmnBrest avatar Oct 28 '19 16:10 dmnBrest

So you managed to track down the problem, but is there a solution for this?

KoichaDev avatar Oct 28 '19 21:10 KoichaDev

Based on official docs and other sources I think we need to "ping" in regular intervals...

https://api.slack.com/rtm

Clients should try to quickly detect disconnections, even in idle periods, so that users can easily tell the difference between being disconnected and everyone being quiet. Not all web browsers support the WebSocket ping spec, so the RTM protocol also supports ping/pong messages. When there is no other activity clients should send a ping every few seconds. To send a ping, send the following JSON:

{ "id": 1234, // ID, see "sending messages" above "type": "ping", ... }

But how do we do it using slackbots?

mkt1988 avatar Nov 17 '19 02:11 mkt1988

I don't know if this is still relevant, but I fixed this problem by adding to the source code a reconnect method. I was told by Slack Help Center that this is normal, and that simply what you need to do is call a method on the api to tell Slack that your app is still running. The reconnect method simply calls the method on the real time messaging api (rtm.connect) which to my understanding sends over a new url to set as your web socket. My pull request didn't go through due to some weird error, and it doesn't seem like anyone is still keeping tabs on this repository. For more clarification take a look at the slack-api documentation here: https://api.slack.com/methods/rtm.connect

Clotonervo avatar Apr 16 '20 20:04 Clotonervo