Hi all,

I'm pretty new to jQuery, and have no idea where to start with this issue that I am having.

What I am trying to do is this:

On my website, members are allowed to send messages to each other.
What I would like to happen is that when the message is successfully sent, I want the recipient to get a little popup notification that x-member has just sent them an email.

Now, I have the messaging system in place, and working properly (the messaging script is written in PHP, and the messages are stored in MySQL). So all that is left to do is for me to figure out how to notify the member of the new message.
Also, and this part is important, I only want the notification to appear one time (meaning that if the member navigates to another page, the alert should not reappear).

I know how to do this in PHP, but I would rather not do it that way for a couple reasons:

1: I don't want to require the page to be refreshed in order for the new message alert to appear
2. I just want to start learning jQuery!

Can anyone give me ideas on where to start, or maybe code samples that I could study?

random thoughts

I have been looking at the jQuery docs, and trying to wrap my head around just the flow of this program.

Does this sound correct, or is my thought process off?

Member sends an email >>> The message is validated and sent in PHP >>> if the message is successfully sent, insert the message into database >>> have an AJAX script that checks the database for new messages every x-amount of seconds >>> if a new message was found for the member, trigger an alert of some type

Recommended Answers

All 9 Replies

You can use an ajax() call to poll a PHP script (which checks your database) to see if there are any messages. You can then return the info in JSON format.

I think that I can figure out how to get that part working, but the part that has me stumped is, how would I have the ajax function triggered only when necessary (as soon as a message is sent), rather than having the notification be displayed every time a page is loaded?

I'd suggest to keep the ajax in background searching for new messages, when a new message is found it'll be shown to the user and then marked as 'readed' in the database.

This way the user will only be notified until the message is readed.

And to make sure that the user readed the message, you could do like facebook, when the new message arrives just add an small notification on the screen, but the message will be marked as readed only when the user clicks it and see the details.

Member Avatar for stbuchok

AleMonteiro, push notifications don't need you to constantly be polling for information from the database. If you have 1000 conncurrent users all polling for messages on a 1 second loop, that's 1000 hits to the DB per second.

If those 1000 users only send an average of 2 messages per minute, then there are only 2 hits to the DB per minute.

So, try to find a way in PHP to do push notifications. If you have the option of using websockets, then try that to create your push.

However, if this is an intranet application and you will only have a few users and can do the polling every 10-30 seconds (or even once a minute or so), then polling should work fine.

Yeah, that is a big concern for me; I want to avoid having any script querying the DB every 10 - 60 seconds. Hopefully at some point I will have anywhere between a few hundred and a few thousand members online at any given time, so I would like to be as effecient as possible...

Is anyone fimiliar with the MySQL TRIGGER function? I need to go research that since I have never used it before, but couldn't that help me out in some way?

Nevermind regarding SQL Trigger, it isn't what I thought it was...

Alright, well I've made an ajax script that works exactly how it should (pretty proud of that! I'm new to ajax/javascript). It queries the database and only looks for new messages posted within the last 30 seconds (I have a time_sent column that records when each message was sent).

Now, the only thing left to figure out is the issue of it constantly hitting the DB.
Right now, I have the setTimeout at 30000, which means the script is querying the DB every 30 seconds.
Unless I can think of a workaround to that, this is the best that it will be; if the site gets as big as I hope it will, I'll have to get a dedicated server and throw more hardware at it...but the programmer in me realizes that that is not the ideal solution :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.