JILL24

JILL24

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!

Showing Posts 1 to 8

namxam

namxam

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.

Awlexus

Awlexus

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

D4no0

D4no0

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.

JILL24

JILL24 OP

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?

sodapopcan

sodapopcan

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.

JILL24

JILL24 OP

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?

LostKobrakai

LostKobrakai

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.

JILL24

JILL24 OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews