33

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.

Robert Harvey
  • 168,684
  • 43
  • 314
  • 475
smaclell
  • 4,482
  • 7
  • 39
  • 49
  • The link is not working – Serob_b Apr 27 '17 at 21:09
  • 1
    Looks like the link died quite a while ago. Here is a copy from the internet archives: https://web.archive.org/web/20110611075502/http://uwmike.com:80/articles/2008/01/22/browser-data-push/ – smaclell Apr 28 '17 at 17:42

9 Answers9

13

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 :)

Oddman
  • 3,083
  • 1
  • 13
  • 12
  • If you look at the push link provided it is esentially a longer poll that trickles data back to the caller slowly which is effectively long polling but can send data immediately when it is ready to mimmic a push. – smaclell Jan 05 '13 at 22:23
  • Ah no worries - I just didn't see it mentioned in any of the resopnses, which I found odd :) – Oddman Jan 06 '13 at 23:21
  • @Oddman Is there a difference between Poling described in an accepted answer and Long Poling? – Serob_b Apr 27 '17 at 21:09
  • 1
    @Serob_b yes. Polling is an instant request done say, every second, every 5 seconds, whatever. Long polling means you hold a connection open and wait for a response, maybe for as long as a minute. The benefit is one of performance - far less requests hitting your server, with a better user experience (the user will get a response as soon as one is available rather than waiting for the next poll) – Oddman May 02 '17 at 02:17
10

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.

cloudworks
  • 589
  • 1
  • 3
  • 18
Supertux
  • 7,198
  • 10
  • 39
  • 46
  • 16
    Sometimes I think I must be the only one that enjoys creating wheels – Mr Bell Nov 02 '10 at 20:14
  • 10
    One thing is enjoying it. Another thing is having the time and resources to dedicate to it when you could use the existing one :) – Pierre Henry Jun 10 '11 at 16:24
  • 2
    yep, on a lot of projects, I would think about how much I could learn from coding from scratch. Two weeks later, I still haven't started the actual project. – gotta have my pops Oct 23 '12 at 07:07
  • On personal projects I code from scratch as much as possible, at least for things I haven't done before. But when time is important, don't reinvent the wheel, buy it(or borrow if it's FOSS) from someone else. – lassombra Jul 22 '13 at 07:07
7

Both have diferent requirements and address diferent scenarios.

If you need realtime updates, like in an online chat, push is a must.

But, if the refresh period is big, as it is in your case (5 minutes), then pool is the appropriate solution. Push, in this case, will require a lot of resource from both the client and the server.

Tip! try to make the page that checks the pool fast and clean, so it doesn't consumes a lot of resources in the server in each request. What I usually do is to keep a flag in memory (like in a session variable) that says if the pool is empty or not... so, I only do havy look in the pool only if it is not empty. When the pool is empty, which is most of the time, the page request runs extremely fast.

Daniel Silveira
  • 37,165
  • 32
  • 96
  • 120
5

Because using a push requires an open HTTP connection to be maintained between your server and each client, I'd go for poll as well - not only is that going to consume a lot of server resources but it's also going to be significantly more tricky to implement as matt b mentioned.

My experience with polling is that if you have a frequent enough polling interval on a busy enough site your web server logs can get flooded with poll requests real quickly.

Edit (2017): I'd say your choices are now are between websockets and long polling (mentioned in another answer). Sounds like long polling might be the right choice based on the way the question mentions that the notifications don't need to be received in real time, an infrequent polling period would be pretty easy to implement and shouldn't be very taxing on your server. Websockets are cool and a great choice for many applications these days, sounds like that might be overkill in this case though.

mmacaulay
  • 2,913
  • 21
  • 27
3

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

matt b
  • 132,562
  • 64
  • 267
  • 334
1

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?

Lou Franco
  • 83,503
  • 14
  • 127
  • 183
1

I haven't tried it myself, but some say COMET works and is easier than you think. There's also a Ruby on Rails plug-in called Juggernaut that I've heard talked about highly. Again, I haven't used it, so YMMV, but my understanding is that it takes far fewer resources compared to polling. I believe (can someone confirm?) that COMET is how MacRumorsLive.com delivers live blogging of WWDC Stevenotes.

Alper Turan
  • 1,010
  • 10
  • 23
Andrew Hedges
  • 21,058
  • 16
  • 62
  • 78
1

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.

savetheclocktower
  • 1,333
  • 6
  • 10
1

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

Kirill
  • 3,485
  • 4
  • 28
  • 34