webhook 和 websocket 的区别 [英] differences between webhook and websocket

查看:131
本文介绍了webhook 和 websocket 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想进行实时聊天.

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

我多年前在 PHP+Ajax+Mysql 中做过这件事,但破坏了我的服务器.然后我尝试用 Flash+ 一个文本文件.我放弃了,10 年没有尝试过.但最近我听说了 webhooks 和 websockets.他们似乎都是这样做的一种方式,但我不太明白其中的区别.谁能解释一下?

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?

推荐答案

Webhooks

Webhooks 用于服务器到服务器的通信.它们通过一台服务器告诉另一台服务器它希望在发生某些事情时将数据发送到某个 url 来工作.

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.

这篇文章讲了一些用途流行服务中的网络钩子.该组织谈论了很多关于在 RESTful API 上下文中使用它们的内容.

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(通常)用于服务器到浏览器的通信.服务器托管一个 websocket 服务器,客户端可以打开到该服务器的连接.这现在很流行,主要是因为它比解决问题的旧方法更快,占用资源更少,例如 长轮询/彗星.

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.

可以使用 websockets 连接 2 个服务器,但通常情况并非如此用于.

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

即使其中一个是(完全)服务器-服务器,一个是(主要)浏览器-服务器,但这些技术经常在相同的地方讨论,几乎就像它们在解决相同的问题一样.如果你向上看这个链,你会发现它们都解决了实时"通信的问题,但是它们以截然不同的方式解决了这个问题的不同方面.

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.

可能存在直接比较的一种情况是,如果您正在构建将由第三方服务器使用的 API.在这种情况下,您可以提供 webhook APIwebsocket API.两者都允许第三方快速获取更新:

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:

  • 如果您选择网络钩子,该第三方仍然需要想办法将您告诉他们的更改推送到他们客户的浏览器.
  • 如果您提供 websocket API,第三方只需设置他们的网站,让他们的每个用户直接连接到您的 websocket API,他们的服务器就可以减少工作量.

这篇关于webhook 和 websocket 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆