0

I'm trying to understand whether the HTML5 Web Notifications API can help me out, but I'm falling short in understanding how it works.

I'd like user_a to be able to send user_b a message within my webapp.

I'd like user_b to receive a notification of this.

Can the web notifications API help here? Does it let me specifically target a user (rather than notify everyone the site has been updated_? I can't see how I would create an alert for one person.

Can anyone help me understand a little more?

pierre
  • 1,235
  • 1
  • 13
  • 30
  • Some good references: https://developer.mozilla.org/en-US/docs/Web/API/notification, http://www.paulund.co.uk/html5-notifications and http://alxgbsn.co.uk/2013/02/20/notify-js-a-handy-wrapper-for-the-web-notifications-api/ – RRikesh Oct 31 '13 at 09:50
  • Sorry to say, but I have read all of these and they don't bring me closer to answering my question. – pierre Oct 31 '13 at 09:53

3 Answers3

1

The notifications API is client side, so it needs to get events from another client-side technology. Here, read THIS: http://nodejs.org/api/. Just kidding. Node.js+socket.io is probably the best way to go here, you can emit events to one or all clients (broadcast). That's a push scenario. Or each user could be pulling their notifications from the server.

humbolight
  • 680
  • 4
  • 13
0

HTML5 Web Notifications API gives you ability to display desktop notifications that your application has generated. What you are trying to achieve is a different thing and web notification is just a part of your scenario.

Depending upon how you are managing your application, for chat and messaging purpose as humbolight mentioned, you should look into node.js. it will provide you the necessary back-end to manage sending and receiving messages between users.

To notify a user that (s)he has received a message, you can opt for ajax polling on client side. Simply create a javascript that pings the server every x seconds and checks if there is any notification or new message available for this user. If response is successful, then you can use HTML5 notification API to show a message to user that (s)he has a new message. The main problem with long polling is server load, and bandwidth usage even when there are no messages, and if number of users are in thousands then you can expect your server always busy responding to poll calls.

An alternate is to use Server Sent Events API, where you send a request to server and then server PUSHES the notifications/messages to the client as soon as they are available. This reduces the unnecessary client->server polling and seems much better option in your case. To get started you can check a good tutorial at HTML5Rocks

Ali
  • 151
  • 11
0

What you're looking for is WebSocket. It's the technology that allows a client (browser) to open a persistent connection to the server and receive data from it at the server's whim, rather than having to "poll" the server to see if there's anything new.

Other answers here have already mentioned node.js, but Node is simply one (though arguably the best) option for implementing websockets on your server. You might also be comfortable with Ratchet, which is a websocket server library for PHP, or Tornado which is in Python.

How you handle your real-time communication is up to you. Websockets are merely the underlying technology that you can use to pass data back and forth. The client side of this will be fairly easy, but on the server side, you'll need a mechanism for websocket handlers to get information from each other. Look at tools like ZeroMQ for handling queues, and Memcached or Redis to handle large swaths of data which don't need to be stored permanently.

ghoti
  • 45,319
  • 8
  • 65
  • 104