简体   繁体   English

如何在Twitter中实现实时通知?

[英]How are real-time notifications implemented in twitter?

How twitter page queries/receives notifications, information about new tweets? twitter页面如何查询/接收通知,有关新推文的信息? I'd like to implement something like this mechanism for my html+js client->webservice 我想为我的html + js client-> webservice实现类似这种机制的东西

I don't know what Twitter uses exactly but there are few techniques to handle server notifications. 我不知道Twitter究竟使用了什么,但是处理服务器通知的技术很少。 You can use long-polling (your client issues the same ajax request every few seconds to get new information): http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery/ 您可以使用长轮询(您的客户端每隔几秒发出相同的ajax请求以获取新信息): http//techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and -jquery /

Or there is the "new" standard called Websocket . 或者有一个名为Websocket的“新”标准。 A good start to how to write a websocket client is this mozilla tutorial . 这个mozilla教程是如何编写websocket客户端的良好开端。

There are multiple ways to implement real-time notifications: 有多种方法可以实现实时通知:

  • HTTP Long Polling : The client initiates a request. HTTP长轮询:客户端发起请求。 The server checks if it has any new notifications. 服务器检查是否有任何新通知。 Irrespective of whether or not it has new notifications appropriate response is send and connection is closed. 无论是否有新通知,都会发送相应的响应并关闭连接。 After time X client initiates another request (+ Very easy to implement - notifications are not real time. They depend on X since data retrieval is client initiated. As X decreases overhead on server increases ) 经过一段时间X客户端发起另一个请求(+非常容易实现 - 通知不是实时的。它们依赖于X,因为数据检索是客户端启动的。因为X减少了服务器增加的开销)

  • HTTP Streaming: This is very similar to HTTP Long Polling however the connection is not closed. HTTP Streaming:这与HTTP Long Polling非常相似,但连接未关闭。 The server sends chunked response. 服务器发送分块响应。 So as soon as server receives new notification that it wants to push it can simply write to the socket. 因此,只要服务器收到想要推送的新通知,它就可以简单地写入套接字。 ( + lower latency than long polling and almost real time behaviour / overhead of closing connection and re opening reduced - memory usage client side keeps on piling up / ugly hacks etc ) (+延迟低于长轮询和几乎实时行为/关闭连接和重新打开的开销减少 - 内存使用客户端继续堆积/丑陋的黑客等)

  • WebSocket: TCP based protocol provides true two way communication. WebSocket:基于TCP的协议提供真正的双向通信。 The server can push data to client any time. 服务器可以随时将数据推送到客户端。 ( + ve: true real time - some older browsers dont support it ). (+ ve:真正的实时 - 一些旧的浏览器不支持它)。 Read more about it WebSocket.org | 阅读更多关于它的信息WebSocket.org | About WebSocket 关于WebSocket

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

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