javascript 实现客户端 WebHook 处理程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12304960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:53:06  来源:igfitidea点击:

Implementing a client-side WebHook handler?

javascripthttppush-notificationwebhooks

提问by Sweety

I am a bit of a newbie in Webhooks, so excuse me if this is a simple question.

我是 Webhooks 的新手,所以如果这是一个简单的问题,请原谅。

I am clear about how Webhook providers work, i.e. whenever this information needing to be pushed, it sends the payload to the URL specified as callback.

我很清楚 Webhook 提供程序是如何工作的,即每当需要推送此信息时,它都会将有效负载发送到指定为回调的 URL。

Now my question is: how do I write a client-side Webhook handler, that can detect/process the callback and update my client-side accordingly. For example, if my client-side is a simple web-page with bullet-points, I would like to append new data to the list, whenever it comes through.

现在我的问题是:如何编写客户端 Webhook 处理程序,它可以检测/处理回调并相应地更新我的客户端。例如,如果我的客户端是一个带有项目符号的简单网页,我想在新数据出现时将其附加到列表中。

Preferably, I would be after a complete JavaScript solution...

最好,我会追求一个完整的 JavaScript 解决方案......

Is there perhaps a JS WebHook Client/Handler that already exists? It seems that this should be so common, that it should exist, although I haven't been able to find anything.

是否可能已经存在一个 JS WebHook 客户端/处理程序?看起来这应该是很普遍的,应该存在的,虽然我一直没能找到任何东西。

回答by matthewhudson

Take a look at WebSockets. Depending on your needs, this could be exactly what you need to avoid polling and keep things in sync - particularly if you have a lots of clients who need to see the same updates from your server.

看看 WebSockets。根据您的需要,这可能正是您避免轮询和保持同步所需要的 - 特别是如果您有很多客户端需要从您的服务器看到相同的更新。

I highly recommend Socket.IO

我强烈推荐Socket.IO

回答by Kristian

To consume a webhook API endpoint, or in other words, to "listen for changes", you'd poll for changes, long-poll for changes, or anything else clever you'd like to do.

要使用 webhook API 端点,或者换句话说,“侦听更改”,您需要轮询更改、长轮询更改或任何其他您想做的聪明的事情。

Or you can use any javascript Publisher Subscriber module to easily do this. try googling around for PubSub stuff. here's an example of one such tool: http://www.pubnub.com/tutorial/javascript-push-api

或者您可以使用任何 javascript Publisher Subscriber 模块轻松完成此操作。尝试在谷歌上搜索 PubSub 的东西。这是一个此类工具的示例:http: //www.pubnub.com/tutorial/javascript-push-api