Best way to check for users new private messages on website?
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开发者_如何学Python!
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.
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.
You can add a field 'messages' to the users table, and increment it when a message is send to an user.
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
精彩评论