botkit: Slack RTM retry/reconnect not working

Hi, I’ve found that the RTM reconnect logic isn’t reached when the RTM connection dies for one reason or another.

Version: 0.1.2 OS: Ubuntu 15.04 Node: v6.1.0

To test I have done the following:

  • Set retry to 5 in my controller config
  • console.log’d the retryEnabled var in Slackbot_worker.js to ensure it was set
  • console.log’d at the start of reconnect()
  • tcpkill’d the RTM connection
  • Confirmed nothing was logged to the console

This follows a series of less drastic actions to confirm it wasn’t working.

It seems the only path to the retry logic is at the bottom of bot.closeRTM when an err object is passed. This code is not reached from the error or close event of bot.rtm which is the path followed when the connection dies unexpectedly. I am unsure whether or not it is desirable to call reconnect() from close, so I’ve not submitted a PR. I’m happy to once it’s been clarified what would be desired. As a user I would probably expect the retry logic to occur on a team_migration_started event also, though I suspect that would be be immediately followed by a close event as per the Slack documentation.

Thank you for botkit, it’s great stuff!

Cheers, Gary.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 10
  • Comments: 15 (3 by maintainers)

Most upvoted comments

For anyone looking for a reliable solution, I have auto-reconnect disabled and use the following code to keep the bot alive. It hasn’t failed once since my last reply here.


var controller = ...;
var bot = ...;

function start_rtm() {
        bot.startRTM(function(err,bot,payload) {
                if (err) {
                        console.log('Failed to start RTM')
                        return setTimeout(start_rtm, 60000);
                }
                console.log("RTM started!");
                });
        });
}

controller.on('rtm_close', function(bot, err) {
        start_rtm();
});

start_rtm();

Hope it helps!

Edit: There’s no need to handle team_migration_started, it’s almost immediately followed by an rtm_close, and the reconnection logic will (within a minute or two, since the first reconnect attempt usually fails) connect you to the new server.

About retry stuff, I have workaround by setting retry when calling spawn instead of in botkit config, var bot = controller.spawn({ token: token, retry: 10 });

however, my issue is the same with @rdohms’s error: Error: Stale RTM connection, closing RTM bot was died without reconnect, I tried to increase ping/pong check time from 1200 to bigger value, it seems to be fine

Thanks! Your code works fine 👍