push-style notifications similar to Facebook with Rails and jQuery

﹥>﹥吖頭↗ 提交于 2019-12-30 01:26:09

问题


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...


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4743536/push-style-notifications-similar-to-facebook-with-rails-and-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!