Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Needs pinging to keep bot(s) alive #50

Open
Battleroid opened this issue Dec 6, 2018 · 8 comments
Open

Needs pinging to keep bot(s) alive #50

Battleroid opened this issue Dec 6, 2018 · 8 comments

Comments

@Battleroid
Copy link

This may need some sort of periodic task that sends a ping over the websocket to prevent bots from disconnecting. See "Ping and Pong".

I've seen it done like the following:

async def keepalive(self):
    while True:
        log.info(f'sending ping {self._ping_count}')
        await self.ping_ws()

async def ping_ws(self):
    await asyncio.sleep(60)
    self._ping_count += 1

    await self.ws.send(
        json.dumps({
            'id': self._ping_count,
            'type': 'ping'
        })
    )

with the keepalive task created shortly after startup like so:

self.loop.create_task(self.keepalive())
@Battleroid
Copy link
Author

Battleroid commented Dec 6, 2018

I ended up hackily putting something together that did the above in my bot that uses slack-sansio and it seems to have to kept it from disconnecting. I spoke too soon. Should probably expose the websocket connection on the slack object somewhere.

@ovv
Copy link
Member

ovv commented Dec 6, 2018

Thanks for the link, I don't think that existed at the time. Do you know if this is required or optional ? I will have to thing of a way to include that for all IO implementation.

In the meantime I suggest you subclass the aiohttp implementation and change the _rtm function to expose the websocket object. Something like:

from slack.io.aiohttp import SlackAPI

class ClientWithWS:
    async def _rtm:
        async with self._session.ws_connect(url) as self.ws:
            async for data in self.ws:
                if data.type == aiohttp.WSMsgType.TEXT:
                    yield data.data
                elif data.type == aiohttp.WSMsgType.CLOSED:
                    break
                elif data.type == aiohttp.WSMsgType.ERROR:
                    break

@Battleroid
Copy link
Author

Battleroid commented Dec 6, 2018 via email

@ovv
Copy link
Member

ovv commented Dec 6, 2018

Ok thanks, I'll have to test it out. I have stopped using the websocket connection a long time ago as the HTTP events API is much more reliable (on a side note there is a linked project helping with that: https://github.com/pyslackers/sir-bot-a-lot-2/)

@Battleroid
Copy link
Author

Battleroid commented Dec 6, 2018 via email

@iMerica
Copy link
Member

iMerica commented Dec 6, 2018

Most mature websocket clients & servers do ping/pong health checks automatically.

@Battleroid
Copy link
Author

@iMerica in that case it might just be something broke for my bot instead, in which case this issue can probably be ignored

@ovv
Copy link
Member

ovv commented Dec 7, 2018

@iMerica it's a custom slack JSON ping. From the documentation it looks like it's optional. I think the issue is slack closing the connection after a while, a goodbye event should be received before closing.

Not sure if adding the ping would tell slack to not close it 🤔

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants