Real-time database monitor

J

Justin Voelker

I have built a php website with a messaging system where users of a
website can send messages to each other and they will appear in the
other users inbox for viewing next time they log in. In a bar at the
top of the page it says "Inbox (0)" if they have no new messages and,
yup, you guessed it "Inbox (2)" if they have two waiting messages. If
someone sends them a message while they are sitting on some page of
the website I would like the counter to increase. Each time someone
sends a message it creates a new record in a single table in the
database. What I am looking for is a way to monitor that database and
find any records with "msg_to" equal to the user id of the person
logged in and "msg_unread" equal to '1.' Right now the mysql query
runs each time a page is loaded so they would be notified then but I
would like it to somehow update the inbox number while they sit on a
page. Does anyone have any ideas? I was told in the comp.lang.php
group that AJAX might be able to help and that I should check this
group. Thank you in advance for your help!
 
L

Laser Lips

Justin, this is really simple to do.

Write a PHP script (e.g getMailCount.php ) and in it put your code
which gets the count of new messages for the user, where user id the
value you pass to this script, probably using $_REQUEST["UserID"] or
somthing similar.

This PHP script needs to echo the number of NEW messages, so at the
end of this script say somthing like 'echo $numberOfNewMessages;'

You will need to call this script with the user id in the URL so
somthing like blar/blar/getMailCount.php?UserID=234

Using Ajax, you call this script. You get the return value and you
set the contents of the DIV containing the 'new mail number' to the
value that the script returns.

Graham
 
E

Erwin Moller

Justin Voelker schreef:
I have built a php website with a messaging system where users of a
website can send messages to each other and they will appear in the
other users inbox for viewing next time they log in. In a bar at the
top of the page it says "Inbox (0)" if they have no new messages and,
yup, you guessed it "Inbox (2)" if they have two waiting messages. If
someone sends them a message while they are sitting on some page of
the website I would like the counter to increase. Each time someone
sends a message it creates a new record in a single table in the
database. What I am looking for is a way to monitor that database and
find any records with "msg_to" equal to the user id of the person
logged in and "msg_unread" equal to '1.' Right now the mysql query
runs each time a page is loaded so they would be notified then but I
would like it to somehow update the inbox number while they sit on a
page. Does anyone have any ideas? I was told in the comp.lang.php
group that AJAX might be able to help and that I should check this
group. Thank you in advance for your help!

Hi,

I saw you posting in comp.lang.php.
Yes, XMLHttpObject (AJAX) is a good solution for this.

Be aware however that this only works for visitors with JavaScript
enabled. While this is the vast majority, it isn't 100%.
(I keep hearing different percentages, but most are over 90%)

If you decide to do it with AJAX, then this is a good place to start:
http://www.w3schools.com/ajax but many other resources exist.

If you want it to work for the world at large (= also for those without
JavaScript), you might want to use a frame or an IFrame.
In this IFrame you place your inbox thingy.
You can use META tags in the HTML document to order it to refresh every
X seconds.

Using AJAX will look smoother than using a refreshing IFrame, because
the page in the IFRame must refresh every X seconds, while AJAX will
only update some content on your page (No IFrames needed).

Hope that helps.

Regards,
Erwin Moller
 
J

Justin Voelker

Justin Voelker schreef:




Hi,

I saw you posting in comp.lang.php.
Yes, XMLHttpObject (AJAX) is a good solution for this.

Be aware however that this only works for visitors with JavaScript
enabled. While this is the vast majority, it isn't 100%.
(I keep hearing different percentages, but most are over 90%)

If you decide to do it with AJAX, then this is a good place to start:http://www.w3schools.com/ajaxbut many other resources exist.

If you want it to work for the world at large (= also for those without
JavaScript), you might want to use a frame or an IFrame.
In this IFrame you place your inbox thingy.
You can use META tags in the HTML document to order it to refresh every
X seconds.

Using AJAX will look smoother than using a refreshing IFrame, because
the page in the IFRame must refresh every X seconds, while AJAX will
only update some content on your page (No IFrames needed).

Hope that helps.

Regards,
Erwin Moller

Thank you for the great help! Because of the nature of the website
and the other various tools available to the users, they must have
javascript enabled to even view the site (the entire site is based
around a good map which needs js). I looked at the w3schools ajax and
I think I understand their examples bt I do have one more question. I
have a limited knowledge of javscript (just enough to be dangerous).
I see in the w3schools examples they reply on the "onchange" event of
a dropdown. What would I need to do since I can't user an "onchange"
event or anything else that requires user interaction? My only
thought is something that executes the command every X seconds, but
wouldn't that cause a lot of server load if there are lots of users?
If this is what I need to do, what code would I use. Thank you again
for the wonderful help!
 
E

Erwin Moller

Justin Voelker schreef:
Thank you for the great help! Because of the nature of the website
and the other various tools available to the users, they must have
javascript enabled to even view the site (the entire site is based
around a good map which needs js). I looked at the w3schools ajax and
I think I understand their examples bt I do have one more question. I
have a limited knowledge of javscript (just enough to be dangerous).
I see in the w3schools examples they reply on the "onchange" event of
a dropdown. What would I need to do since I can't user an "onchange"
event or anything else that requires user interaction? My only
thought is something that executes the command every X seconds, but
wouldn't that cause a lot of server load if there are lots of users?
If this is what I need to do, what code would I use. Thank you again
for the wonderful help!

Hi,

Yes, I agree 100% with your analysis.
You need an approach that checks every X second.
The function you need is window.setTimeout().
Look it up somewhere. ;-)

And yes, if X is small, you create a lot of work for the server.
That is the price you pay if you have many users polling their inbox
every X seconds. Nothing to do about that (in this setup).
You need a completely other technology to let the server talk to the
webpage (which would eliminate all the polling).
That can be dome but is a lot harder than AJAX.

I think the smartest move is to make X large, eg 60 seconds.
I could live with the fact that my email is only checked every 1 minute. ;-)

One warning:
Make sure you set use the setTimeout() command after your AJAX response
is in. Otherwise you might get yourself in the mess that you have
multiple calls to your PHP script (during high serverload eg).
That can also be handled, but it is much easier to avoid it.
So if your XMLhttpObject has readystate=4, update your inbox, then set a
new timeout. (If you have no clue what I am talking about, read the
tutorial first on w3schools.)

Good luck.

Regards,
Erwin Moller
 
J

Justin Voelker

Justin Voelker schreef:





Hi,

Yes, I agree 100% with your analysis.
You need an approach that checks every X second.
The function you need is window.setTimeout().
Look it up somewhere. ;-)

And yes, if X is small, you create a lot of work for the server.
That is the price you pay if you have many users polling their inbox
every X seconds. Nothing to do about that (in this setup).
You need a completely other technology to let the server talk to the
webpage (which would eliminate all the polling).
That can be dome but is a lot harder than AJAX.

I think the smartest move is to make X large, eg 60 seconds.
I could live with the fact that my email is only checked every 1 minute. ;-)

One warning:
Make sure you set use the setTimeout() command after your AJAX response
is in. Otherwise you might get yourself in the mess that you have
multiple calls to your PHP script (during high serverload eg).
That can also be handled, but it is much easier to avoid it.
So if your XMLhttpObject has readystate=4, update your inbox, then set a
new timeout. (If you have no clue what I am talking about, read the
tutorial first on w3schools.)

Good luck.

Regards,
Erwin Moller

Thank you Erwin! I got it working. It may not be the prettiest thing
ever but not fully understanding AJAX yet it at least works. Thanks
again for the help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top