Client notification, should I use an AJAX Push or

2019-01-10 11:04发布

I am working on a simple notification service that will be used to deliver messages to the users surfing a website. The notifications do not have to be sent in real time but it might be a better user experience if they happened more frequently than say every 5 minutes. The data being sent to and from the client is not very large and it is a straight forward database query to retrieve the data.

In reading other conversations on the topic it would appear that an AJAX push can result in higher server loads. Since I can tolerate longer server delays is it worth while to have the server push notifications or to simply poll.

It is not much harder to implement the push scenario and so I thought I would see what the opinion was here.

Thanks for your help.

EDIT: I have looked into a simple AJAX Push and implemented a simple demo based on this article by Mike Purvis. The client load is fairly low at around 5k for the initial version and expected to stay that way for quite some time.


Thank you everyone for your responses. I have decided to go with the polling solution but to wrap it all within a utility library so that if they want to change it later it is easier.

9条回答
聊天终结者
2楼-- · 2019-01-10 11:44

Definitely use push its much cooler. If you just want simple notifications I would use something like StreamHub Push Server to do the heavy-lifting for you. Developing your own Ajax Push functionality is an extremely tricky and rocky road - you have to get it working in all browsers and then handle firewalls and proxies killing keep-alive connections etc... Why re-invent the wheel. Also, it has a similarly low footprint of less than 10K so it should suit if that is a priority for you.

查看更多
3楼-- · 2019-01-10 11:44

I would implement a poll just because it sounds simpler to write, and keeping it simple is very valuable.

查看更多
\"骚年 ilove
4楼-- · 2019-01-10 11:45

Not sure if you have taken a look at some of the COMET implementations out there (is that what you mean by AJAX push).

If the user is surfing the site, won't that in effect be requesting information from the server that this notification can piggy-back on?

查看更多
Anthone
5楼-- · 2019-01-10 11:49

I'm surprised noone here has mentioned long-polling. Long polling means keeping an open connection for a longer period (say 30-60 seconds), and once it's closed, re-opening it again, and simply having the socket/connection listen for responses. This results in less connections (but longer ones), and means that responses are almost immediate (some may have to wait for a new polling connection). I'd like to add that in combination with technologies like NodeJS, this results in a very efficient, and resource-light solution, that is 100% browser compatible across all major browsers and versions, and does not require any additional tech like Comet or Flash.

I realize this is an old question, but thought it might still be useful to provide this information :)

查看更多
Juvenile、少年°
6楼-- · 2019-01-10 11:52

It's impossible to say whether polling will be more expensive then pushing without knowing how many clients you'll have. I'd recommend polling because:

  • It sounds like you want to update data about once per minute. Unless notifications are able to arrive at a much faster rate than that, pushing would mean you're keeping an HTTP connection open but seeing very little activity on it.
  • Polling is built on top of existing HTTP conventions, so any server that talks to web browsers is already ready to respond to ordinary Ajax requests. A Comet– or Flash socket–based solution has different requirements; you'll need something like cometd on the server side and a client-side library that groks server-side push.

So if you needed something heavy-duty to manage a torrent of data and a crapload of clients, I'd recommend Comet. But that doesn't seem to be the case.

查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-10 11:55

There's now a service http://pusherapp.com that is trying to solve this problem once and for all, in a blink. Might be worth checking out. (disclaimer: i am in no way associated with them).

查看更多
登录 后发表回答