views:

43

answers:

4

I am coding a website in PHP and Javascript which implements private messaging functionality, and i wanted to have it so when a user recieved a new message it notifies them (by a flashing 'mail' icon at the top of the screen).

The question is how to implement this functionality without having to check the messages database for unread messages to the specified user every time they load a page, it seems like too much server load for every user to perform on every pageload.

Can anybody suggest a more efficient method? It doesn't matter if the user isn't notified instantly upon recieving a new message.

Thanks!

A: 

You can have a new field added to the table of users which gets set to true when ever a new message arrives into his inbox. That way upon receiving messages the flag gets set and you'll never actually do a query to check for unread messages.

Am
A: 

You can add a field 'messages' to the users table, and increment it when a message is send to an user.

Mewp
A: 

I would say that it's likely not too much server load to check the messages table for a new message at every page load.

With proper indexing, this shouldn't be a concern at all.

$0.02

Ian P
+1  A: 

To avoid doing it every page load you could have some javascript within a timer function that calls to the database and checks to see if the user received messages. As Am said if you just have a flag set on the user table you can just do a

select message_flag from tbl_usrs where usr_id = $usr_id

Now that said a select statement like that by itself being ran every time a page loads really won't increase the load on your server that much, unless you are running a site with significant amounts of users.

Mike Keller
Would i have to set the message_flag back to 0 after this though?I don't know too much about server loads etc, so thanks for informing me - i have been coding with efficiency in mind so much that i've probably been going OTT with it ha
Tommo
You wouldn't want to set the message_flag back to the default until after the user has visited the message box or alternatively you could also not reset the flag till after they've viewed all their unopened messages. I would probably go with the second method and assign each entry in the messages table a viewed flag of some sort.My methodology when putting together a new (personal) project is to get it done first, then when it breaks somewhere fix it. For clients I put more thought into how it's all going to work and what the requirements are before going overboard with scalability etc.
Mike Keller