How to build realtime push notification feature li

2019-02-01 15:15发布

问题:

I'm going to build realtime push notification feature for my web application ( a small social network) and I don't know where to start.

This is what I want to build: there are like buttons, comment forms, ... Users click like, write their comments and (relatively) immediately, on the owner's browser shows the number of new likes and comments, ... Something like that.

I've read about socketIo on nodeJs, MeteorJS but unfortunately, they need WebSocket supported by mordern browsers. I've just read about Comet technic and find it pretty easy to apply. But i'm not sure it will performs well because Comet relies on long-polling connection (correct me if I'm wrong).

In addition, I think facebook is using Comet for its push notification feature. Through console tab on firebug plugin I can see there's alway a holding connection to facebook. So can anybody show me a technic, a model to develop a feature like that?

回答1:

A promising idea is to work with the HTML5 notification API; it's perfect if you want notifications to pop on the user screen as long as his browser is running (even if you're surfing another website or if all windows are closed).

http://www.paulund.co.uk/html5-notifications

However, if what you want is to update different parts of your page asynchronously (without refreshing or pushing a button), you should use together :

  • Ajax calls;
  • Listeners and observers.

When you Ajax calls retrieve particular types of json data (for example), it can trigger appearance of a badge (listener) with a number of new notifications, or so...

With JQuery installed, you should be fine...

Even though it's often not the case, sometimes, for simple tweaks, it's easier to code the job done...

You can start here :

How implement a "observer" in Jquery without plugins? (it's old, but interesting)

Or see this page :

browser instant updates with ajax/jquery

(incredible how often google queries return stacko' pages)



回答2:

You should check out MQTT. It basically works on the Publish-Subscribe model and is very easy to use. This protocol has a small footprint and consumes less bandwidth. Facebook's Messenger uses MQTT too.



回答3:

you can use an ajax call coming into (for example) a php script on the server, which keeps the connection open and only replies if and when something needs to be displayed to the user. should nothing happen within a certain time, the connection gets closed and the client fires a new ajax call. note that this only addresses the client/server communication, you would still need a notification method inside the server to wake up the php script if you want to avoid having a script constantly polling the database, but there are quite a lot of soultuions to this and they depend on what language you use on the server.



回答4:

I have got an idea, it is simple but it may work. Why don't you hide notification bar as Div tag and design it with css to make it look like notification bar. Then whenever some user likes or comments about the page, write php or js function and connect it to like or comment submit button that will reveal page owners invisible div to visible. And I believe , depending on what you use, I would probably prefer php session() to Identify if page owner is online and can get notifications. moreover, if you need to track statistics of the page, you may create a small database that holds, page id and user comments. You can use this database to push multiple notifications on that hidden Div. you can use Jquery to make it move like Facebook if you want to. I m not %100 percent sure if this is the most optimized way to do that but it is possible. By the way, I surfed some to see what people use to do that. surprisingly I couldn't find something as well.