How do Discord or Slack bots update messages in real-time using REST API?

I’ve been working on a project where I’m trying to understand how Discord or Slack bots function on the backend. I know bots typically use WebSocket connections to receive real-time updates, and REST APIs to send responses or updates.

My question is: how do bots manage to update messages in real-time? For instance, when a bot edits or updates a message in a channel, it happens almost instantly like WebSockets, but traditionally, this should be happening over a REST API. Similarly, webhooks also seem to perform instant updates, but I understand they also use REST APIs.

Does anyone have insights into how these bots are architected to provide real-time functionality while still using REST APIs for responses? Is there a mechanism behind the scenes that ensures this instant update behavior? Any explanation would be appreciated!

I never built a chat bot and I am not sure what you really ask for. REST APIs can be really fast. You have a little bit of overhead compared to a we socket connection, but they are still fast.
If you are interested in the inner workings of a chat bot, I would suggest to read the code of one of the various elixir discord / slack / … libraries.

Pretty much what has been said above. The cycle really is just “the bot receives an event through the websocket connection, and then the bot creates a new event with the REST API”.

The bot libraries can perform some tricks to reduce the overhead of the process, like keeping a persistent connection to the REST API to avoid having to establish a new connection on every request. But other than that, most of the instantaneousness comes from how their backend is able to distribute the events to the listeners

I am not entirely sure what the question is about, but if you are asking about websocket alternatives that are used to handle events in realtime you basically have the following options:

  1. Using HTTP long polling. The idea behind this is simple, you basically start a HTTP request to a server, leave it running and it will send a response only when new events are happening;
  2. Setting webhooks. Webhooks is the mechanism where you host your own HTTP server. The service you are using can then call your endpoints when an event happens.

PS: These are the mechanisms that are currently in-use by telegram, for the ones you mentioned it might be different.

i am just saying, yesterday i observed a situation where i followed my other discord server announcement channel and i just post a message in announcement channel and publish it and in second server channel, i observer it reaches in real time like websocket is working but we know webhook use rest api. here i want to ask, are they using long pooling in backend or something else? and if long pooling, will it not increase the overhead?

As @namxam said, HTTP is quite fast. I’m also not 100% what you are asking but traditionally REST can be thought to be slower if it does things like validation and persistence before sending a response. However, there is nothing stopping it from sending a immediate response before doing that stuff. I’ve experienced this in these services where it looks like a message was sent only to be notified moments later that it failed.

ok here is the simplfied question,
we know discord use elixir for its chat server and we know bot or webhook use rest api to send response.
now i want to ask, how bot is able to post message to channel in real time by using rest api?

The response is only one part of the question though. Using a webhook commonly means that in this case discord does a http request to an external system on certain conditions. That http request can then make that external system trigger an rest api request back to discord. The external system doesn’t need to be continously connected to discord if discord does drives the inital chain of events happening.

do you not think when external system trigger http request in elixir, it will increase over head?