推送长轮询 spring_构建浏览器推送通知服务http长轮询和Web套接字协议

推送长轮询 spring

Image for post
a gopher flying faster than the plane to deliver a push notification
地鼠的飞行速度比飞机快,可以发出推送通知

构建浏览器推送通知服务-长轮询和Web套接字协议 (Building a Browser Push Notification Service — Long Polling and the Web Socket protocol)

What’s the ideal pattern /design / tech stack for a push notification service (also, what’s a push notification?) (using websocket, also, what’s a websocket?) that would allow efficient usage of resources, be lightning fast, and also scale horizontally? In these series of blogs, lets try to break this down into smaller chunks and answer them individually.

推送通知服务(也就是什么是推送通知?)(使用websocket,又是什么websocket?)的理想模式/设计/技术堆栈是什么?它可以有效利用资源,快速闪电化并水平扩展? 在这些系列博客中,让我们尝试将其分解为较小的块并分别进行回答。

Before we dive deep into the details, lets gets some terminologies clear.

在深入研究细节之前,我们先弄清楚一些术语。

What are websockets / websocket protocol? `WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection`. Lets try to take the sophistication out from the previous sentence, and understand the basics. WebSocket is simply another communication protocol like HTTP, or TCP. HTTP works in a simple client-request - server-response manner, for example, it could be like the militants talking over a radio, where, the personnel sitting at the base asks for information, only after which the person on the field responds with the answers, and the other way around is not allowed. (You never question your superiors in the army :P)

什么是websockets / websocket协议? WebSocket 是一种计算机 通信协议 ,可 通过单个 TCP 连接 提供 全双工 通信通道 。 让我们尝试从前面的句子中删除复杂性,并了解基本知识。 WebSocket只是另一种通信协议,例如HTTP或TCP。 HTTP以一种简单的客户端请求-服务器响应的方式工作,例如,就像武装分子通过无线电通话,坐在基地的人员要求提供信息,然后现场人员进行响应答案,反之亦然。 (您从不质疑您的上司:P)

WebSocket protocol allows both the client (or as we call in this article, the target) and the server to send any piece of information to each other at any point in time, provided the connection is established. Similar to you talking to your friend over a call, where either of you can talk and /or listen, once the call is placed, anytime.

WebSocket协议允许客户端(或我们在本文中称为目标)和服务器在建立连接的任何时间点彼此发送任何信息。 类似于您通过电话与朋友聊天,一旦拨打电话,您就可以随时聊天和/或收听。

What is a push notification? As far as this blog is concerned, a push notification is simply some piece of information / data you want to convey to your target client, without the client explicitly asking for it. Like for example, when you open your mail, anytime you get a new mail, you would like to see a popup on the top of the tab that says ‘You have a new mail’. Now here, the browser tab (our client / target) did not ask the backend if you have a new mail. Rather, the backend realized that someone sent you a mail, and now wants to send you this information / notification. This type is what we’re calling push notification here.

什么是推送通知? 就此博客而言,推送通知只是您要传达给目标客户的一些信息/数据,而客户没有明确要求。 例如,当您打开邮件时,无论何时收到新邮件,您都希望在选项卡顶部看到一个弹出窗口,上面写着“您有新邮件”。 现在,在这里,浏览器选项卡(我们的客户端/目标)没有询问后端是否有新邮件。 相反,后端意识到有人向您发送了邮件,现在想向您发送此信息/通知。 这种类型就是我们在这里所说的推送通知。

The Use Case: Now lets talk of a use case. Say, you have a desktop dashboard. Now, you want to enable push notifications from the backend servers to this desktop UI (which is opened on a browser tab). This is slightly different compared to the usual requirements out of APIs, where the UI (target) makes a specific request to our servers and the data is sent back as a response of it (classic use case of HTTP protocol). For push notifications, however, the client has not made a request, but instead, the backend is the initiator and it said, hey, I want to send this information (notification) that is important for the target (UI) to see right away, like we saw for the new email notification. Lets see how we can enable this.

用例 :现在讨论一个用例。 假设您有一个桌面仪表板。 现在,您要启用从后端服务器到此桌面UI(在浏览器选项卡上打开)的推送通知 。 与API的通常要求稍有不同,在API中, UI ( 目标 )向我们的服务器发出特定请求,然后将数据作为响应发送回去(HTTP协议的典型用例)。 但是,对于推送通知,客户端尚未发出请求,而是后端是发起方,它说,嘿,我想发送此信息 ( 通知 ),该信息对于目标(UI)立刻可见很重要,就像我们在新电子邮件通知中看到的一样。 让我们看看如何启用它。

Push Notification with long polling: Now that we get the peculiarity of this requirement, let us brainstorm about how we can get this done. One simple way could be long polling. Meaning, the target simply sends an HTTP request to the backend. The backend, instead of responding to the request immediately, just keeps it in pending state. Now, only when there is a push notification to be sent to a target, the backend server figures the pending HTTP requests sent by that target and now, sends the push notification as a response. This is actually how the push notifications were handled traditionally. For multiple reasons, in the smart-ass-land, this method is now looked down upon, mostly because of how intensive it turned out to be on the server side.

长时间轮询推送通知 :现在我们已经知道了这一要求的特殊性,让我们就如何完成此工作进行集思广益。 一种简单的方法可能是长时间 轮询 意思是,目标只是将HTTP请求发送到后端。 后端无需立即响应请求,而只是将其保持在待处理状态。 现在,仅当有推送通知要发送到目标时,后端服务器才能计算出该目标发送的未决HTTP请求,然后将推送通知作为响应发送。 实际上,这就是传统上处理推送通知的方式。 由于多种原因,在智能资产领域 ,此方法现在被人们看不起,主要是因为它在服务器端的强度很高。

Push Notification with WebSockets: There’s a better solution possible. Say, how about we just register and persist with the server, the connection aganist the targetID instead? Meaning, the target UI, as soon as is fired up, a connection is made and persisted with the push-notification service. The target, keeps listening over this connection, if there’s any new notification and then processes it accordingly. The target and the push-notification service can both keep polling at effective intervals to make sure the connection is functional for robustness. If the connection is detected dead by the target, it can simply make a new connection request. And if the server detects a dead connection, it simply cleans the connection related objects from it’s memory. This exact philosophy is harvested in the websocket protocol.

使用WebSockets推送通知:有更好的解决方案。 假设我们只注册并坚持使用服务器,而连接代理改为targetID呢? 意思是,目标用户界面一被启动,便建立连接并与推送通知服务保持一致。 如果有任何新的通知,目标将继续监听此连接,然后对其进行相应的处理。 目标和推式通知服务都可以保持有效间隔进行轮询,以确保连接具有鲁棒性。 如果目标检测到连接已死,则可以简单地发出新的连接请求。 而且,如果服务器检测到连接中断,它只会从内存中清除与连接相关的对象。 这个确切的原理是从websocket协议中获得的。

How is the WebSocket communication channel created, an example? Imagine this like you are walking up to a bank, and requesting to open a Savings Account, and at the same time, requesting an upgrade for thisnewly created account that allows you to have a credit card as well. The bank will then authenticate your documents, check for your CIBIL score and approve or deny your request accordingly. If approved, an account will be created and a credit card alloted, so you can make transactions over it. Also, if you fail to make transactions for a consecutive period of three months say, your account may be marked dormant and later closed.

例如,如何创建WebSocket通信通道? 想象一下,这就像您要去银行,并要求开设一个储蓄帐户,同时又要求升级这个新创建的帐户一样,使您也可以使用信用卡。 然后,银行将对您的文件进行身份验证,检查您的CIBIL分数,并相应地批准或拒绝您的请求。 如果批准,将创建一个帐户并分配一张信用卡,以便您可以通过该帐户进行交易。 另外,如果您说连续三个月都无法进行交易,则您的帐户可能会被标记为Hibernate状态,之后便被关闭。

How is the WebSocket communication channel actually created? Similarly, for a websocket connection to be established, the target makes an HTTP request to the server backend, with something called an ‘Upgrade’ header set to convey that the client wants to upgrade the communication protocol to WebSocket. The server then checks for the authentication credentials, and accordingly approves the upgrade / denies it (and closes the connections). If the upgrade is approved, the target gets a confirmation after which it can continuously listen over this connection for any new notifications. In addition, there is continuous polling to ensure that the connection has not gone ‘dormant’ or like we call it, it has not become a dead conenction.

实际如何创建WebSocket通信通道? 类似地,对于要建立的Websocket连接,目标向服务器后端发出HTTP请求,并设置了一个名为“ Upgrade ”标头,以表示客户端希望将通信协议升级为WebSocket。 然后,服务器检查身份验证凭据,并相应地批准升级/拒绝身份验证(并关闭连接)。 如果升级获得批准,则目标将得到确认,之后目标可以继续在此连接上侦听任何新通知。 此外,还会进行连续轮询以确保连接不会“ Hibernate ”或像我们所说的那样一直保持无效连接。

In conclusion, for multiple reasons, the websocket connections turn out be efficient in terms of resource consumption on the server side, greatly reducing the costs. The details about the comparison about the ways to enable push notification, can be in itself, another article. But for now, in the following articles, we’ll try to address the challenges faced when developing a websocket server (here).

总之 ,出于多种原因,Websocket连接在服务器端的资源消耗方面非常有效,从而大大降低了成本。 关于启用推送通知的方式进行比较的详细信息本身可以在另一篇文章中。 但是现在,在以下文章中,我们将尝试解决开发Websocket服务器( 此处 )时面临的挑战。

翻译自: https://medium.com/swlh/building-a-browser-push-notification-service-http-long-polling-and-the-web-socket-protocol-5e83cd1420c1

推送长轮询 spring

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值