让用户在社交网络应用程序中保持同步?
Keeping users in sync with each other in an social network app?
我想知道在社交网络中保持用户彼此同步的最佳方式是什么。相关堆栈是一个带有 NodeJS 后端的 iOS 应用程序。让我举个例子:
假设 X 和 Y 是社交网络上的朋友。 Y 的帖子出现在 X 的提要中,因此,Y 缓存在 X 的 phone 上的某处。然而,今天早上,Y 决定更改个人资料照片。一切正常,新的头像上传到服务器了,但是怎么让X知道头像变了呢?
我可能的解决方案: 创建一个包含一堆 "cookies" 的路由 /<UID>/updates
,让用户知道自上次以来更改了什么和谁更改了他们向路由发出了 GET 请求。
这看起来很优雅,但让我担心的是客户端发生了什么(我是否应该在我的应用正常运行期间每 2 分钟发出一次 GET 请求?)。还有其他解决方案吗?
一种解决方案确实是轮询服务器,但这不是很优雅。更好的方法是使用 websockets:
WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
它们是客户端和服务器之间的双向连接,允许服务器通知客户端任何更改。例如,这是 Meteor 框架中使用的底层技术。
看看 this blogpost for an example of how to use websockets between an iOS client and a NodeJS backend. They make use of the open source SocketRocket iOS 图书馆。
我想知道在社交网络中保持用户彼此同步的最佳方式是什么。相关堆栈是一个带有 NodeJS 后端的 iOS 应用程序。让我举个例子:
假设 X 和 Y 是社交网络上的朋友。 Y 的帖子出现在 X 的提要中,因此,Y 缓存在 X 的 phone 上的某处。然而,今天早上,Y 决定更改个人资料照片。一切正常,新的头像上传到服务器了,但是怎么让X知道头像变了呢?
我可能的解决方案: 创建一个包含一堆 "cookies" 的路由 /<UID>/updates
,让用户知道自上次以来更改了什么和谁更改了他们向路由发出了 GET 请求。
这看起来很优雅,但让我担心的是客户端发生了什么(我是否应该在我的应用正常运行期间每 2 分钟发出一次 GET 请求?)。还有其他解决方案吗?
一种解决方案确实是轮询服务器,但这不是很优雅。更好的方法是使用 websockets:
WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
它们是客户端和服务器之间的双向连接,允许服务器通知客户端任何更改。例如,这是 Meteor 框架中使用的底层技术。
看看 this blogpost for an example of how to use websockets between an iOS client and a NodeJS backend. They make use of the open source SocketRocket iOS 图书馆。