Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

differences between webhook and websocket

I've always wanted to do a real-time chat.

I've done that years ago in PHP+Ajax+Mysql and broke my server. Then I tried with Flash+ a text file. I gave up and haven't tried in 10 years. But recently I heard about webhooks and websockets. And they both seem to be a way to do that but I don't really quite grasp the difference. Anyone can explain?

like image 806
David 天宇 Wong Avatar asked Apr 19 '14 16:04

David 天宇 Wong


People also ask

What is the difference between webhook and API?

An application programming interface (API) is a software interface that serves as a bridge between computers and applications. A webhook is a way for one application to deliver data to another app in real-time.

What is the difference between webhook and poll SCM?

While polling and webhooks both accomplish the same task, webhooks are far more efficient. Zapier found that over 98.5% of polls are wasted. In contrast, webhooks only transfer data when there is new data to send, making them 100% efficient.

What are some differences between WebSockets and HTTP?

WebSocket is an event-driven protocol, which means you can actually use it for truly realtime communication. Unlike HTTP, where you have to constantly request updates, with websockets, updates are sent immediately when they are available.


1 Answers

Webhooks

Webhooks are for server to server communication. They work by one server telling another server that it wants data sent to a certain url when something happens.

This article talks about some uses of webhooks in popular services. This organization talks a lot about using them in the context of RESTful APIs.

Websockets

Websockets are (usually) for server to browser communication. The server hosts a websocket server, and clients can open a connection to that server. This is popular now mostly because it is faster and less resource-hogging than older ways of solving the problem, like long-polling/COMET.

It is possible to connect 2 servers using websockets, but that is not usually what they are used for.

The confusion

Even though one of these is (exclusively) server-server and one is (mostly) browser-server, these technologies are often discussed in the same places, almost like they are solving the same problems. If you look up the chain high enough, you see that they both solve the problem of "real time" communication, but they solve different aspects of this problem in very different ways.

One situation where there may be a direct comparison is if you are building an API that will be consumed by a third party server. In that situation, you could provide a webhook API or a websocket API. Both allow the third party to get updates quickly:

  • If you choose webhooks, that third party will still have to figure out a way to push the changes you are telling them about to their client's browsers.
  • If you provide a websocket API, the third party can just set up their site so each of their users connects directly to your websocket API, and their servers have to do less work.
like image 52
turtlemonvh Avatar answered Sep 22 '22 19:09

turtlemonvh