views:

238

answers:

5

I'm already tossing around a solution but as I haven't done something like this before I wanted to check what SO thought before implementation.

Basically I need to modify an existing web based application that has approximately 20 users to add push notifications. It is important that the users get the notifications at the same time (PC-A shouldn't get an alert 20 seconds before PC-B). Currently the system works off of AJAX requests, sending to the server every 20 seconds and requesting any updates and completely rebuilding the table of data each time (even if data hasn't changed). This seems really sloppy so there's two methods I've come up with.

  1. Don't break the connection from server-client. This idea I'm tossing around involves keeping the connection between server and client active the entire time. Bandwidth isn't really an issue with any solution as this is in an internal network for only approximately 20 people. With this solution the server could push Javascript to the client whenever there's an update and modify the table of data accordingly. Again, it's very important that every connected PC receives the updates as close to the same time as possible. The main drawback to this is my experience, I've never done it before so I'm not sure how well it'd work or if it's just generally a bad idea.

  2. Continue with the AJAX request, but only respond in intervals. A second solution I've thought of would be to allow the clients to make AJAX requests as per usual (currently every 20 seconds) but have the server only respond in 30 second intervals (eg 2:00:00 and 2:00:30 regardless of how many AJAX requests it recieves in that span of time). This would require adjusting the timeout for the AJAX request to prevent the request timing out, but it sounds okay in theory, at least to me.

This is for an internal network only, so bandwidth isn't the primary concern, more so that the notification is received as close to each other as possible. I'm open to other ideas, those are just the two that I have thought of so far.

Edit

Primarily looking for pros and cons of each approach. DashK has another interesting approach but I'm wondering if anyone has experience with any of these methods and can attest to the strengths and weaknesses of each approach, or possibly another method.

+7  A: 

If I understand well your needs I think you should take a look to Comet

Comet is a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it. Comet is an umbrella term, encompassing multiple techniques for achieving this interaction. All these methods rely on features included by default in browsers, such as JavaScript, rather than on non-default plugins.

The Comet approach differs from the original model of the web, in which a browser requests a complete web page at a time.

Claudio Redi
+1 - So long as your server can maintain a bunch of open connections, this is a good way to go. Your client code just says "give me an update", and your server holds the connection open until there's an update to give. As soon as the client receives the update, it initiates another "give me an update" request.
timdev
Yeah I looked at that, with long polling being the closest to my two ideas. My question more so was the benefits of doing one way or another as well as stability. Basically, is it a good/bad idea to keep AJAX requests open for 30 seconds at a time?
Robert
Have a look at ape-project, I think this project will suit your needs very well.
Chris
+3  A: 

How about using an XMPP server to solve the problem?

Originally designed to be an Instant Messaging platform, XMPP is a messaging protocol that enables users in the system to exchange messages. (There's more to this - But let's keep it simple.)

Let's simplify the scenario a little bit. Imagine the following:

You're a system admin. When the system has a problem, you need to let all the employees, about 20 of them, know that the system is down.

In the old days, every employee will ask you, "Is the system up?" every hour or so, and you'll response passively. While this works, you are overloaded - Not by fixing system outage, but by 20 people asking for system status every hour.

Now, AIM is invented! Since every employee has access to AIM, you thought, "Hey, how about having every single one of them join a 'System Status' chat room, and I'll just send a message to the room when the system is down (or is back)?" By doing so, employees who are interested in knowing system status will simply join the 'System Status' room, and will be notified of system status update.

Back to the problem we're trying to solve...

  • System admin = "System" who wants to notify the web app users.
  • Employees = Web app users who wants to receive notification.
  • System Status chat room = Still, system Status chat room

When web app user signs on to your web app, make the page automatically logs them onto the XMPP server, and join the system status chat room.

When system wants to notify the user, write code to logon to the XMPP server, join the chat room, and broadcast a message to the room.

By using XMPP, you don't have to worry about:

  • Setting up "Lasting connection" - Some open source XMPP server, eJabberd/OpenFire, has built-in support for BOSH, XMPP's implementation of the Comet model.
  • How the message is delivered

You however will need the following:

  • Find a Javascript library that can help you to logon to an XMPP server. (Just Google. There're a lot.)
  • Find a XMPP library for the server-side code. (XMPP library exists for both Java & C#. But I'm not sure what system you're using behind the scene.)
  • Manually provision each user on the XMPP server (Seems like you only have 20 people. That should be easy - However, if the group grows bigger, you may want to perform auto-provisioning - Which is achievable through client-side Javascript XMPP library.)

As far as long-lasting AJAX calls, this implementation is limited by the at-most-2-connection-to-the-same-domain issue. If you used up one connection for this XMPP call, you only have 1 more connection to perform other AJAX calls in the web-app. Depending on how complex your webapp is, this may or may not be desirable, since if 2 AJAX calls have already been made, any subsequent AJAX call will have to wait until one of the AJAX pipeline freed up, which may cause "slowness" on your app.

You can fix this by converting all AJAX calls into XMPP messages, and have a bot-like user on the server to listen to those messages, and response to it by, say, sending back HTML snippets/JSON objects with the data. This however might be too much for what you're trying to achieve.

Ahh. Hope this makes sense... or not. :p

DashK
This is an interesting idea, +1, I'll look into this
Robert
A diagram to go with the text: http://tinypic.com/r/5b66hw/7 (Excuse my drawing skill.)
DashK
+1 for your drawing skill!
aSeptik
A: 

In addition to the other two great options above, you could look at Web Workers if you know they have latest Chrome, Safari, FF, or Opera for a browser.

A Worker has the added benefit of not operating in the same thread as the rest of the page, so performance will be better. The downside is that, for security purposes, you can only send string data between the two scripts and the worker does not have window or document context. However, JSON can be represented as a string, so there's really no limit to the data.

Workers can receive data multiple times and asynchronously. You set the onmessage handler to act each time it receives something.

AutoSponge
+1  A: 

See http://ajaxpatterns.org/HTTP_Streaming

It allows You to push data from the server when server wants it. Not just after the query. You could use this technique without making large changes to the current application, and synchronize output by the time on the server.

naugtur
A: 

If you can ask every user to use a specific browser (Latest Safari or Chrome), you can try WebSockets too.

Ashit Vora