DEV Community

Cover image for Webhooks and WebSockets

Webhooks and WebSockets

Ricardo Esteves on April 17, 2024

Introduction In the world of web development, there are numerous technologies and terminologies that developers come across, two of whic...
Collapse
 
bremerlyimo profile image
Bremer Jonathan • Edited

I love your article especially the "Use cases" section. But I think it's worth reminding the reader that you could in theory substitute a Webhook impl with WebSocket. The converse however won't work. The catch is that WebSocket requires you to maintain a TCP connection and that is a waste of resources vis-a-vis the Webhook use cases.

Collapse
 
ricardogesteves profile image
Ricardo Esteves

Thank you a lot Bremer, I'm glad you liked it!
Yes, that's true, it depends on each use case. WebSocket is more complex solution, and you need to maintain a stable connection by a TCP protocol. If is just needed some kind of "event listener" to get real time updates for example instead of polling an API (and get rate limited) webhook is definitely the best solution.

Collapse
 
anmolbaranwal profile image
Anmol Baranwal

I've seen webhook endpoints in several applications but never really read about it.
I loved the use cases along with the clear explanation and of course the shout-out system. 😁

Collapse
 
ricardogesteves profile image
Ricardo Esteves

thanks Anmol, yes I have noticed that there is indeed some confusion when it comes to these two technologies. They are often confused.

Collapse
 
lucasmedina profile image
Lucas Medina • Edited

So, would you consider websockets for any implementation that requires two-way client-server communication? For example, in polling scenarios? I assume that webhooks can't be applied to a client-side environment, right?
Great article, by the way.

Collapse
 
ricardogesteves profile image
Ricardo Esteves

Hey Lucas, thanks for liking the article, I'm glad you liked it!
Yes, you’re spot on. WebSockets are great for two-way chats between a client and server, like in polling scenarios. And if for example, the server needs to push updates to the client in real-time, which is not possible with traditional HTTP requests.
Webhooks, on the other hand, are more for server-to-server communication. For example, if one server needs to notify another when a specific event has occurred, but yes, they’re not designed for client-side environments.

Collapse
 
shyam_10 profile image
Shyam raj

Great.. Article.. Loved it🫴❀️

Collapse
 
ricardogesteves profile image
Ricardo Esteves

Thank you Shyam, glad you liked it! πŸ‘

Collapse
 
dgesteves profile image
Diogo Esteves

Awsome article @ricardogesteves, super useful. 🀩

Collapse
 
ricardogesteves profile image
Ricardo Esteves

Thanks Diogo, glad you liked it! πŸ‘Œ

Collapse
 
faaktap profile image
Fakie Tap

Hi Ricardo.
Nice readable informative article.
What do you think of "long-polling" - would you say that can be used rather than webhooks if you have a very small requirement.
Have you ever used it?

Collapse
 
ricardogesteves profile image
Ricardo Esteves

Hello Fakie, I’m glad you found the article informative.
Long-polling can indeed be used as an alternative to webhooks for smaller requirements. It’s simpler and doesn’t require the setup of a callback endpoint as webhooks do. The main issue for me is that long-polling can be more resource-intensive on the server side because connections are kept open longer.
But if it is a service that you have available for free, sure it could be a simpler solution.