5 Replies - 580 Views - Last Post: 25 August 2010 - 11:53 AM

#1 Lochie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 07-February 10

Live Alerts for my users [Help]

Posted 21 August 2010 - 01:59 PM

So I want to create a live alert system for my users. The basic idea is that one user can change the status of a row in the MySQL database, the row has both that user's ID, and an ID of another user. When the user updates this status (a number between 0 and 9) it will display a corresponding message on the other user's screen. How should I go about this? Would using AJAX be the best approach? I considered Comet, but I don't know how I could efficiently set it up with Comet.

Is This A Good Question/Topic? 0
  • +

Replies To: Live Alerts for my users [Help]

#2 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1105
  • View blog
  • Posts: 6,918
  • Joined: 07-September 06

Re: Live Alerts for my users [Help]

Posted 21 August 2010 - 02:37 PM

While you could use Ajax to get the information loaded from the database and update the page the number of requests you would have to be making to the server (for it to be "live") would have to be a lot more than you likely want to do (as in once a second). If you take into account having 10 users, each of which are viewing the page at the same time and having it check for updates once a second like I said above that turns into 10 hits a second to your site. Then if they are viewing your site for any length of time (such as if it was a social networking site) then you can guess they will be on your site anywhere between 5 and 60+ minutes. For this we will average it (60 + 5 / 2 = 32.5), then you multiply that by 60 to get the number of seconds they are on your site (32.5 * 60 = 1950), and multiply that by the number of users viewing (we are saying 10): 19500. That is almost 20k hits for 10 people viewing the site for 32 minutes, if you were to say get a little more popular (100 users) that would be 195000 hits in 32 minutes. Then if you were to become huge (Facebook size) and say only have 100,000 users on at any given time that would tunr out to be quite a substancial load on the server for just updating status messages or checking if they need to be updated.

Now, lets look at the size of responses you would send. The easiest would be if there was no update, at which point you could either reply with nothing, or use the current status. The size of this data would be the size of the headers + the size of the status (or headers + 0, if not sending anything). If the status did change the size would be headers + size of message. This would get more important when looking at character encoding. For instance UTF-8 and UTF-16 are different size character block being sent, and as such take different sizes even if the same message is being sent.

The way I would suggest doing it (which will be more work, but lover the amount of server stress you should experience) is to have a program running in the background acting as a socket server. Then have a flash or HTML5 JS socket opened up to the socket server and use that to listen and dispatch status updates to all users who are going to be effected by the update. This will allow you to quickly send a single message to each socket connected and save the status to the database without having to continually ping the site (if you use TCP/IP and don't worry about checking for dropped connections on a regular interval it will further reduce the server load). If you were to do it in this way you could reduce your message size to size of status (number of characters), and reduce requests to the server substantially in comparison to using an Ajax ping style update.

Hope that makes sense
Was This Post Helpful? 1
  • +
  • -

#3 Lochie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 07-February 10

Re: Live Alerts for my users [Help]

Posted 21 August 2010 - 04:09 PM

View PostBetaWar, on 21 August 2010 - 01:37 PM, said:

While you could use Ajax to get the information loaded from the database and update the page the number of requests you would have to be making to the server (for it to be "live") would have to be a lot more than you likely want to do (as in once a second). If you take into account having 10 users, each of which are viewing the page at the same time and having it check for updates once a second like I said above that turns into 10 hits a second to your site. Then if they are viewing your site for any length of time (such as if it was a social networking site) then you can guess they will be on your site anywhere between 5 and 60+ minutes. For this we will average it (60 + 5 / 2 = 32.5), then you multiply that by 60 to get the number of seconds they are on your site (32.5 * 60 = 1950), and multiply that by the number of users viewing (we are saying 10): 19500. That is almost 20k hits for 10 people viewing the site for 32 minutes, if you were to say get a little more popular (100 users) that would be 195000 hits in 32 minutes. Then if you were to become huge (Facebook size) and say only have 100,000 users on at any given time that would tunr out to be quite a substancial load on the server for just updating status messages or checking if they need to be updated.

Now, lets look at the size of responses you would send. The easiest would be if there was no update, at which point you could either reply with nothing, or use the current status. The size of this data would be the size of the headers + the size of the status (or headers + 0, if not sending anything). If the status did change the size would be headers + size of message. This would get more important when looking at character encoding. For instance UTF-8 and UTF-16 are different size character block being sent, and as such take different sizes even if the same message is being sent.

The way I would suggest doing it (which will be more work, but lover the amount of server stress you should experience) is to have a program running in the background acting as a socket server. Then have a flash or HTML5 JS socket opened up to the socket server and use that to listen and dispatch status updates to all users who are going to be effected by the update. This will allow you to quickly send a single message to each socket connected and save the status to the database without having to continually ping the site (if you use TCP/IP and don't worry about checking for dropped connections on a regular interval it will further reduce the server load). If you were to do it in this way you could reduce your message size to size of status (number of characters), and reduce requests to the server substantially in comparison to using an Ajax ping style update.

Hope that makes sense

While this is an interesting solution, like you said, it requires more work, and in fields I'm in experienced in. Would the AJAX request not just be loading the requested data, as opposed to the entire website? 1 request per second, times n users, times 1 byte of data and the size of the header = (1+headers)n bytes of data. So if I have 1000 users online, it will not be all that much memory being used.

This post has been edited by Lochie: 21 August 2010 - 04:11 PM

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,851
  • Joined: 18-April 07

Re: Live Alerts for my users [Help]

Posted 21 August 2010 - 04:27 PM

I think BetaWar was giving you the perspective of what the server has to do to service so many users all requesting data every second. Just the fact that the server has to process a simple request and return a result can be a bit much and doesn't scale that well. You have to remember that the Internet is stateless and requests take time to resolve, get processed and returned. One second is just not all that feasible in most situations. Google through their entire resources behind the idea in Google Wave and still it was sluggish. I think if you made the request every 30 seconds or so, that would still appear to be very responsive for the user and also ease things up a bit for the server. Your users won't exactly know the real time message they are getting is actually 30 seconds stale *at most*.

The only other option is to leave a persistent connection open (which eats up resources on the server) or have it listen for data and your server push the request to all clients it knows it has out there (like you would an email list).

:)
Was This Post Helpful? 1
  • +
  • -

#5 Lochie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 07-February 10

Re: Live Alerts for my users [Help]

Posted 21 August 2010 - 05:16 PM

View PostMartyr2, on 21 August 2010 - 03:27 PM, said:

I think BetaWar was giving you the perspective of what the server has to do to service so many users all requesting data every second. Just the fact that the server has to process a simple request and return a result can be a bit much and doesn't scale that well. You have to remember that the Internet is stateless and requests take time to resolve, get processed and returned. One second is just not all that feasible in most situations. Google through their entire resources behind the idea in Google Wave and still it was sluggish. I think if you made the request every 30 seconds or so, that would still appear to be very responsive for the user and also ease things up a bit for the server. Your users won't exactly know the real time message they are getting is actually 30 seconds stale *at most*.

The only other option is to leave a persistent connection open (which eats up resources on the server) or have it listen for data and your server push the request to all clients it knows it has out there (like you would an email list).

:)

Ok, thanks for your input guys. I think changing the request to 30 seconds should be fine. Cheers.
Was This Post Helpful? 0
  • +
  • -

#6 KuroTsuto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 42
  • View blog
  • Posts: 182
  • Joined: 13-February 09

Re: Live Alerts for my users [Help]

Posted 25 August 2010 - 11:53 AM

Hey,

I realize you've already resolved your course of action, but I have a suggestion based on that of BetaWar's. To that end, you would be using HTML5 sockets with a Flash fallback for browsers that don't support sockets.

Sounds complicated with much to learn, of course, but by using a beta service that has surfaced on the internet but a year ago or so, it should take you no more than an hour to learn, I should think. The service is called the Pusher App, available at http://www.pusherapp.com. While you have to request a beta invite, the queue seems incredibly short right now, and I received mine before the day's end.

After I understood the system, it took all of 30 lines of jQuery and 8 of HTML to create a fully-functional live chatroom. Pusher App has a fairly large selection of resources, such that you may acquire all of the Javascript and PHP scripts/libraries necessary to communicate with their service without writing them on your own.

This option may well be preferable for you for a number of reasons. One is that it is essentially next-to-realtime... Another is that it will save your server from the bandwidth and processing resources required by AJAXing a database query once per user per 30 seconds.

Anyway, you should check it out. Definitely something to take note of, or worth an appraisal.

Cheers,
~KuroTsuto

This post has been edited by KuroTsuto: 25 August 2010 - 11:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1