webhook 网络钩子
引入
当你的应用想从其他别的应用获取消息,你有两种方式。
一是总是轮询其他的应用,以期获得是否有某事件发生的消息;
二是在其他应用上配置webhook URL,当其他应用上有相关事件发生时,会主动推送消息到你的应用
There are two ways your apps can communicate with each other to share information: polling and webhooks.
- Polling is like knocking on your friend’s door and asking if they have any sugar (aka information), but you have to go and ask for it every time you want it.
- Webhooks are like someone tossing a bag of sugar at your house whenever they buy some. You don't have to ask, they just automatically punt it over every time it's available.
Webhooks are automated messages sent from apps when something happens. They have a message—or payload—and are sent to a unique URL(named webhook URL)—essentially the app's phone number or address which is configured by user. Webhooks are almost always faster than polling, and require less work on your end.
以生活中的例子来看:
They're much like SMS notifications. Say, your bank sends you an SMS when you make a new purchase. You already told the bank your phone number (this is like that you have configured a webhook url to the bank), so they knew where to send the message. They type out "You just spent $10 at NewStore" and send it to your phone number +1-234-567-8900. Something happened at your bank, and you got a message about it. All is well.
webhook
Webhooks are "user-defined HTTP callbacks"
webhook的功能是,就像当你的银行卡余额变动时,手机能直接获得短信推送一样,当一个app上发生了某个事件event时,可以主动推送消息到yourapp上
实现webhook,简单来说需要两步
- 配置你的webhook url到webhook provider上。要想让别的app(webhook provider)主动推送消息给yourapp,你就得先把收消息的地址告诉别的app,就像你先把电话号码告诉银行一样
- webhook url需要遵循webhook provider的规范。对于开放给用户配置的webhook url,webhook provider会有统一的规范,就像银行需要大家都配置合法的手机号码。例如,会规范参数的格式,例如所有webhook url都必须接受这样的参数:
{
"time":
"msg":
}
不然的话,你的webhook url接受一类参数,他的webhook url接受另一类参数,那webhook provider就没办法统一处理了
The data format of webhook is usually JSON. The request is done as an HTTP POST request.
网友评论