简体   繁体   English

Websockets 与 iOS 推送通知

[英]Websockets vs. iOS Push Notifications

Take an iOS app like Instagram.以 Instagram 之类的 iOS 应用为例。 Instagram is fundementally a real-time application that updates its UI whenever a user interacts with you. Instagram 从根本上说是一个实时应用程序,只要用户与您交互,它就会更新其 UI。 For example, if someone likes your post and you are using the app, the UI is updated to trigger dopamine release and inform you that something has hapened to one of your posts.例如,如果有人喜欢您的帖子并且您正在使用该应用程序,则 UI 会更新以触发多巴胺释放,并通知您您的某个帖子发生了某些事情。 Similarly, when someone sends you a direct message on instagram, while using the app, you see the message spring down from the top, all in real-time.同样,当有人在 Instagram 上向您发送直接消息时,在使用该应用程序时,您会看到从顶部向下的消息 spring,所有这些都是实时的。

In terms of implementing such real time features it is obvious that a naive HTTPS polling approach is far too inefficient.在实现这样的实时功能方面,很明显,一个简单的 HTTPS 轮询方法效率太低了。 Thus this leaves two strategies:因此,这留下了两种策略:

1.) APNS Push Notifications: 1.) APNS 推送通知:

When a user likes a post, sends a direct message, comments (etc.), send an HTTP POST to a backend server that will then update the database and send a silent Apple push-notification to the device of the recipient.当用户喜欢帖子、发送直接消息、评论(等)时,将 HTTP POST 发送到后端服务器,然后后端服务器将更新数据库并向收件人的设备发送静默 Apple 推送通知。 The recipient, which is using the app, will receive the pushed payload and will send an HTTP GET to the backend server to fetch the needed data (ie. the contents of the direct message sent).使用该应用程序的接收者将接收推送的有效负载,并将发送 HTTP GET 到后端服务器以获取所需的数据(即发送的直接消息的内容)。 The UI is updated in quasi "real-time". UI 是准“实时”更新的。

2.) Websockets: 2.) 网络套接字:

Whenever any user opens the iOS app, connect the user to the server via a websocket.每当任何用户打开 iOS 应用程序时,通过 websocket 将用户连接到服务器。 This means, that all users currently using the app are connected to the server via their own websocket.这意味着,当前使用该应用程序的所有用户都通过他们自己的 websocket 连接到服务器。 When a user likes a post, sends a direct message, comments (etc.), the app sends a message to the server through the socket indicating the action.当用户喜欢帖子、发送直接消息、评论(等)时,应用程序会通过套接字向服务器发送一条消息,指示该操作。 The server, before updating the database, finds the socket associated with the recipient and forwards the message through the socket to the recipient.服务器在更新数据库之前,找到与接收者相关联的套接字,并通过套接字将消息转发给接收者。 Upon reception of the message, the UI is updated in real-time收到消息后,UI 会实时更新

Which of these approaches is scalable and better suited for a production environment?这些方法中的哪一种具有可扩展性并且更适合生产环境?

TL;DR You'll love websockets + Combine framework, event driven to update your UI smoothly. TL;DR 你会喜欢 websockets + 组合框架,事件驱动来顺利更新你的 UI。 APNS can be somewhat unreliable in terms of deliverability and resource/database costs, and to me is more of an information center. APNS 在可交付性和资源/数据库成本方面可能有些不可靠,对我来说更像是一个信息中心。

Reasons to like websockets:喜欢 websocket 的原因:

  • Decreases DB costs (eg finding device to send to)降低数据库成本(例如寻找发送到的设备)
  • You know when the user is not using the app, decreasing outbound data costs您知道用户何时不使用应用程序,从而降低出站数据成本
  • Latency and faster practically speaking (more in depth in the websocket paragraph)实际上,延迟和更快(websocket 段落中更深入)

To answer the question: websockets are scalable based on your user count obviously.回答这个问题:websockets 显然可以根据您的用户数量进行扩展。 APNS is not server side testable in the CI, not cross platform, and not exactly feasible in terms of resource consumption. APNS 不能在 CI 中进行服务器端测试,不能跨平台,在资源消耗方面也不完全可行。


I have a bias with websockets.我对 websockets 有偏见。 But to present why I like it more, think about the efficiency given by your Instagram example.但是要说明我为什么更喜欢它,请考虑一下您的 Instagram 示例给出的效率。

To me, websockets == Event Driven, APNS is a simple information center.对我来说,websockets == 事件驱动,APNS 是一个简单的信息中心。

APNS: APNS:

You sign up with APNS and you save the device in your backend.您使用 APNS 注册并将设备保存在后端。 Great, every time an action is performed, like someone liking your post, query your backend, find the device that is linked to the OP, then send it outbound (which costs money if you think cloud computing costs).太好了,每次执行操作时,例如有人喜欢您的帖子,查询您的后端,找到链接到 OP 的设备,然后将其发送到外部(如果您认为云计算成本,这需要花钱)。 And you have to do that every single time someone likes your post (obviously you can aggregate, but why bother when you have websockets?).每次有人喜欢你的帖子时,你都必须这样做(显然你可以聚合,但是当你有 websockets 时为什么还要麻烦呢?)。 So database costs is one thing to think about .所以数据库成本是需要考虑的一件事 Additionally, a user could be muting their notifications.此外,用户可能正在静音他们的通知。 I don't have Instagram, but something efficiency wise they could have done (if not by sockets) is updating their UI based on incoming notifications for likes/hearts rather than a websocket connection.我没有 Instagram,但他们本可以做的一些提高效率的事情(如果不是通过套接字)是根据收到的喜欢/心的通知而不是 websocket 连接来更新他们的 UI。 That's slow in terms of latency, costly, and unreliable if marked as spam.如果标记为垃圾邮件,则延迟、成本高昂且不可靠,这会很慢。 However, APNS has the benefit of not needing authorization unlike websockets, but...但是,APNS 的好处是不像 websockets 那样需要授权,但是......

Websockets:网络套接字:

When we approach websockets, you only authorize once (at least for mobile applications).当我们接近 websockets 时,您只授权一次(至少对于移动应用程序)。 You're removing outbound data costs (in terms of $$ and latency) by removing stuff like headers.您通过删除标头之类的内容来消除出站数据成本(以 $$ 和延迟计)。 You want to send small chunks of data, and sockets are just sending text/binary (I like to create commands out of them using JSON. A notable example is GitHub).您想发送小块数据,而 sockets 只是发送文本/二进制文件(我喜欢使用 JSON 从中创建命令。一个值得注意的例子是 GitHub)。 When your user is done with the app, you close the connection and you don't need to send anymore data via APNS.当您的用户使用完该应用程序后,您将关闭连接,并且您不再需要通过 APNS 发送数据。 Your server itself can be communicating with a single websocket via something like Redis PubSub in order to update your UI when someone uses a POST request to heart a post without needing to send some push notification.您的服务器本身可以通过 Redis PubSub 之类的方式与单个 websocket 进行通信,以便在有人使用 POST 请求来发帖而不需要发送一些推送通知时更新您的 UI。 That's a win for the liker (who doesn't need to wait for that push notification to be sent) or the other way if you offload it to a background task (the OP doesn't need to wait).这对于喜欢的人(他们不需要等待发送推送通知)或者如果您将其卸载到后台任务(OP 不需要等待)的其他方式是一个胜利。 Websockets == Event Driven: Websockets == 事件驱动:

  • Small data chunks that are constantly delivered (as in a lot) is much better than having APNS in which it can be slow to actually deliver, especially if you're flooding Apple/your user with notifications, marking you as spam.不断交付的小数据块(如很多)比拥有实际交付速度可能很慢的 APNS 要好得多,特别是如果您向 Apple/您的用户发送通知,将您标记为垃圾邮件。 Although, disclaimer, that's just my personal belief and speculation depending on your use case.尽管免责声明,这只是我个人的信念和推测,具体取决于您的用例。
  • You're removing those unnecessary database costs.您正在消除那些不必要的数据库成本。
  • A downside would be keeping the connection alive.缺点是保持连接有效。

Personally, I've used websockets with the combine framework recently for a chat application, but it can be used in so many different circumstances.就个人而言,我最近将 websockets 与 combine 框架一起用于聊天应用程序,但它可以在许多不同的情况下使用。 Updating a Facebook feed/comment section, live notifications via the websocket instead of APNS, even posting content.更新 Facebook 提要/评论部分,通过 websocket 而不是 APNS 进行实时通知,甚至发布内容。

APNS is not guaranteed to be delivered, especially if you have a lot of notifications - I don't remember where I read that, but after some threshold per min it'll stop working.不能保证 APNS 会送达,特别是如果您有很多通知 - 我不记得我在哪里读到的,但每分钟达到某个阈值后,它就会停止工作。 Also, you'll have to send them to all your users, not just online ones.此外,您必须将它们发送给您的所有用户,而不仅仅是在线用户。 It's possible to send silent notifications, but it's still not optimal.可以发送静默通知,但仍然不是最佳选择。

That's why websocket is a preferred way.这就是为什么 websocket 是首选方式的原因。 Also you can use something like https://github.com/centrifugal/centrifugo , which is a helper for your server, that'll hold all the connections, and is very stable.您也可以使用https://github.com/centrifugal/centrifugo 之类的东西,它是您服务器的助手,可以保存所有连接,并且非常稳定。

It is not one out of two, even the web-socket isn't failsafe, but all of above should be considered for an effective communication, when the app is in foreground use a web-socket to listen for any updates from the server, expect a confirmation from client when something is delivered by socket, if the web-socket connection is not active or the there is no confirmation response deliver the update through APNS/FCM, as APNS/FCM it is not guaranteed to deliver, deliver the updates when next Socket connection is successful.它不是二选一的,即使网络套接字也不是故障安全的,但是应该考虑以上所有内容以进行有效的通信,当应用程序处于前台时,使用网络套接字侦听来自服务器的任何更新,当通过套接字传递某些内容时,期望客户端确认,如果 web-socket 连接不活动或没有确认响应通过 APNS/FCM 传递更新,因为 APNS/FCM 不能保证传递,传递更新当下一个 Socket 连接成功时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM