类似于 Facebook 的推送式通知,带有 Rails 和 jQuery
我想创建一个像 Facebook 一样的推送通知系统。每当 Facebook 想要告诉你一些事情(比如有人评论了某个帖子、给你加了标签等),你就会在屏幕左下角看到一条小通知。它淡入又淡出。
如何使用 jQuery 和 Rails 构建这样的系统?它是如何运作的? JS 是否会不断询问服务器“有新通知吗?”或者服务器是否以某种方式推送到此服务。
现在,如果一个用户向另一个用户发送一条消息(例如),我可以向用户的队列添加一条通知,说明“您有一条新消息”,但直到页面重新加载后它才会出现......
I want to create a push notification system like Facebook. Whenever Facebook wants to tell you something (like that someone commented on a post, tagged you, etc), you'll see a small notification show up in the bottom left corner of the screen. It fades in and fades out.
How do I build a system like this with jQuery and Rails? How does it even work? Does JS constantly ask the server, "is there a new notification?" or does the server somehow push to this service.
Right now, if a user sends another user a message (for example), I can add a notification to the user's queue saying "you have a new message", but it won't appear until a page reload...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Juggernaut 看起来真的很酷,我很久以前就看过它的演示,现在它是在节点之上重写的。 js.好的。您必须安装 Redis 和 Node.js 并运行 Node.js 服务器,其余的就非常简单了。
Pusherapp.com 看起来也很酷,它是付费的,但至少你不必安装和运行额外的服务(以及守护进程、扩展它们……)。
但是,如果负载不太大,我宁愿让事情变得简单,只使用 javascript 进行轮询。您可以轻松地编写自己的插件(这并不太难),但是 jquery 已经存在一些非常好的插件。例如PeriodicalUpdater。
Juggernaut looks really cool, i saw it demoed a long time ago, now it is rewritten on top of node.js. Nice. You have to install redis and node.js and run a node.js server and the rest is dead-easy.
Pusherapp.com also looks cool, it is paying, but at least you don't have to install and run extra services (and daemonise, scale them, ...).
But, if the load is not too big, i would prefer to keep things simple, and just poll using javascript. You could easily write your own (it is not too hard), but some very good plugins already exist for jquery. For instance PeriodicalUpdater.
目前,此类问题可以通过Comet来解决。
有关更多信息,http://en.wikipedia.org/wiki/Comet_(programming)
基本上浏览器使用 HTTP,这是一种无状态协议,因为它只能以请求然后响应的方式工作,所以我们永远无法真正获得真正的推送通知。当想要推送到浏览器时,我们必须以某种方式模仿 TCP/Socket 之类的连接来推送到它。 Comet 只是用于定义此类技术的术语。
有许多库可以对此提供帮助。例如:Orbited、Juggernaut on Rails。
对于新的浏览器,有一种称为 Websockets 协议的东西,我提到的库也利用了这一点。这是一个广阔的主题,但我很确定,您会找到一些有关 Juggernaut 和 Rails 的基本示例。
还有 http://pusherapp.com,它做同样的事情,但要收费。
Currently, such problems can be solved by using Comet.
For more, http://en.wikipedia.org/wiki/Comet_(programming)
Basically browsers use HTTP which is a stateless protocol and because it only works in a request then response kinda way, we can never really get a real push notification. When wanting to push to the browser we have to somehow mimick that TCP/Socket like connection to push to it. Comet is just a term that is used to define such techniques.
There are many libraries which help in this.eg:- Orbited, Juggernaut on rails.
With new browsers there is something called Websockets Protocol, the libraries I mentioned take advantage of this as well. Its a vast topic, but I am pretty sure, you ll find some basic examples with Juggernaut and Rails.
There is also http://pusherapp.com, that does the same thing but charges money for it.
许多网站实现此类功能的另一种方式是 bosh,即通过同步 http 的双向流:
http://en .wikipedia.org/wiki/BOSH
这是几个 javascript 库之一:
http:// /code.stanziq.com/strope/
这种类型的设置需要后端有一个 xmpp/bosh 服务器,但基本上浏览器保持一个打开的连接(与 comet 非常相似),并且当消息进入浏览器时处理它们。
该技术可用于通知、聊天以及任何您想要实时发生的事情。
Another way a lot of sites achieve features like this is bosh, or bidirectional streams over synchronous http:
http://en.wikipedia.org/wiki/BOSH
Here is one of a few javascript libraries out there:
http://code.stanziq.com/strophe/
This type of setup requires an xmpp/bosh server on the backend, but basically the browser holds a connection open (extremely similar to comet) and as messages come in the browser processes them.
This technique can be used for notifications, chatting, and just about anything that you want to happen in realtime.