Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notifications in Facebook

In Facebook. you get notifications, when somebody sends you a message. This is done without the user doing nothing. How is it triggered?

Can I achieve something like that using jsp, jQuery - ajax ?

How is that possible. Because I normally do things based on key press, or when user refreshes his browser. How can you know when that you receive a message?

like image 349
user478636 Avatar asked May 02 '26 21:05

user478636


1 Answers

There are plenty methods for a server to send messages to a client. Some are:

  • "Comet", where XmlHttpRequest's onreadystatechange is (ab)used to allow the server to 'stream' events, notifications, and other live data to the browser. This is now deprecated, but is the most compatible method. See http://en.wikipedia.org/wiki/Comet_(programming))
  • WebSockets, which allows a HTML5-compatible browser and an appropriate web server to establish a two-way socket. See http://en.wikipedia.org/wiki/WebSockets
  • Server-sent events, like WebSockets, and only available in some browsers. These, unlike WebSockets, do not need a specialised webserver, but only allow the server to push events to the browser, not vice versa. See http://en.wikipedia.org/wiki/Server-sent_events

I would suggest using the jQuery EventSource plugin, which uses server-sent events when available, and, when not available, falls back to Comet: https://github.com/rwldrn/jquery.eventsource

like image 188
Fahad Sadah Avatar answered May 05 '26 12:05

Fahad Sadah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!