Can anvil host an “always on” service?

What I’m trying to do:
I’m researching if anvil can host a discord bot that is always online, waiting for specific user interactions on my discord server, to then perform actions. I read some of the documentation and see that anvil can handle scheduled tasks in intervals down to a minute, but I’d like the bot always running in realtime waiting for specific commands to invoke its functionality. Is a service like that possible on anvil?

1 Like

Welcome to the Forum!

Do you mean something that provides a REST-style API? (See HTTP APIs.)

1 Like

If your discord bot can interact with an API through http requests, then you can create endpoints in the Anvil server to serve responses from anvil on demand.

HTTP Endpoints are designed to do exactly that: Anvil Docs | Creating HTTP APIs

Hmmm this might work. It relies on the discord api to route calls based on user input. I’m not quite sure if it’s a REST-style api on the backend.

Here it explains (I think, I haven’t read it) how to trigger a request to your HTTP endpoint when something happens in Discord.

You can post messages to discord using Web hooks from anvil. I have a few of these myself.

However, you can’t use discord.py to run a full bot that responds to commands at anvil. That’s because an async bot of that sort needs its own event loop.

2 Likes

Actually, I was able to do that with Anvil.

After heroku shut down their free tier plan, I was looking for a reliable place to host my bot. Since I was already paying for Anvil, I thought to myself - why not use Anvil for that?

The basic logic is to create a scheduled task that runs every minute (but since that background task will be up forever typically, the next scheduled will not run).

After a week or two, when the background task vanishes, you’ll have the bot back online in less than a minute. My bot is able to handle almost 4000 servers that way without much issues.

It’s not the best way to host the bot but it does work. Although, you’ll need atleast a Personal Plan (so don’t go for this unless you are already paying Anvil).

5 Likes

I’ve made exactly this, but its late. I’ll write up an example in the morning