Notification service - constantly running

o hai guyz

New member
Jan 15, 2010
917
8
0
I'm trying to add a notification feature to a photosite so users get notifications when people like/comment their photos. Used cron jobs but the minimum update time is 1 minute which means the notification bubble remains there saying you have 5 notifications for a full minute after you check them. I assume there are no servers that offer 1-second update time on cron jobs, so Is there a better way to set this up? I want it to be like a Facebook-style system where the notifications update practically instantly, so as soon as you check them they disappear and the bubble updates to 0.
 


Have you tried something along the lines of AJAX where they just hit it every 5 seconds looking for a "notification" with a timestamp greater than the last one they actually read?

1 notification -> timestamp 5
still unread.

2 minutes later, 5 notifications with timestamps 6 7 8 9 10. Now says 6.

Read first notification, timestamp 5.

Now says 5 notifications, timestamps 6 7 8 9 10 where query looked for greater than 5.

Edit: or what about Comet or Backbone.js?
 
I am confused, what is the cron job actually doing, is it somehow directly interfacing with the client, or updating a database?
 
I am confused, what is the cron job actually doing, is it somehow directly interfacing with the client, or updating a database?

Cron jobs are scripts that run at a given interval. Say you need a script to check a website to see if it is live every 1 hour, you create the script, and setup the script in the cron job are of cpanel, and set it for every one hour.

You can automate a lot of stuff with cron jobs. That's how cache systems, etc empty themselves out, etc.
 
And another thing, Ajax is probably the best way to go with this. It can be set to check every 1/1000 of a second (if your server can handle it), and get the latest messages. And it only runs when the user is online, and it's coming from the user's browser, instead of from the server. Gritter (Growl) is a pretty cool script, and it does everything you are looking for. You will just have to make a setInterval Ajax request, to get out if there are new messages.

boedesign – canadian based web developer eh » Blog Archive » Gritter for jQuery (Growl)

Good luck bro.
 
How is ajax the 'best' way? It provides un-necessary polling on the server (could be heavy strain if you want to poll often), whereas a websocket connection is just pretty much idle until a message is sent.

I do however agree with using Growl, it's pretty damn awesome and I've been using it lately.
 
Cron jobs are scripts that run at a given interval. Say you need a script to check a website to see if it is live every 1 hour, you create the script, and setup the script in the cron job are of cpanel, and set it for every one hour.

You can automate a lot of stuff with cron jobs. That's how cache systems, etc empty themselves out, etc.

We all know what a cron job is

And another thing, Ajax is probably the best way to go with this. It can be set to check every 1/1000 of a second (if your server can handle it), and get the latest messages. And it only runs when the user is online, and it's coming from the user's browser, instead of from the server. Gritter (Growl) is a pretty cool script, and it does everything you are looking for. You will just have to make a setInterval Ajax request, to get out if there are new messages.

boedesign – canadian based web developer eh » Blog Archive » Gritter for jQuery (Growl)

Good luck bro.

are-you-fucking-kidding-me.jpg


Don't listen to this dude unless you want to ddos yourself or melt your user's browsers. If you're going to poll, I would consider 500ms to be the absolute lower limit, but you should really be using websockets well before that.

Use Pusher, it's literally designed for what you want to do and is extremely compatible with all but the shittiest of shitty browser and you can have it up and running in a few minutes.
 
I don't really understand the problem.

Add an 'is_read' column to the notification table in your database, which is either 0 or 1. Have AJAX check every 10 seconds or so for new notifications that have 'is_read = 0'. Then once someone has viewed the notifications, send a quick AJAX call to update the 'is_read' column to 1.

Or am I missing something here? What are you even using cron for? Maybe I'm just not understanding the problem. Regardless, you don't want to run a cron job every second, and you don't want to be sending AJAX calls every 0.001 seconds as suggested above either.

EDIT: Run a cron job every day or so, and purge any read notifications that are over a week old, so the table size doesn't blow up on you.
 
We all know what a cron job is

I have no idea, can you clarify what that is?

I can tell you, depending on traffic AJAX can be just fine. Is it super important that the users find out to the second that their shit got a comment on it? Probably not. So in the case you did overload the server with requests you can push the time back on it, say every 30 seconds or 1 min, or whatever.
 
I am confused, what is the cron job actually doing, is it somehow directly interfacing with the client, or updating a database?

I have no idea, can you clarify what that is?

We all know what a cron job is

Clearly, dchuk, you are mistaken. I was responding to the question posed by mattseh about a cron job.

As well, everything depends on your setup, and the way you are configuring the system. The OP was proposing creating a script that was running every 1 second on the server. Talk about taxing the system. If they are looking for that type of response, it should only run when a client is actually on the system, otherwise, it will be taxing itself during nights, etc. So, logically Ajax was one of the best options to ask every, X amount of seconds whether there is a new message, instead of the server writing to a db, every second forever, even if there is no body on the server. The first response was an Ajax solution, so I continued along that line.

My 1/1000 seconds was more of a "IF YOU REALLY WANT TO" statement, as you can see from the next statement "(if your server can handle it)". I would recommend setting it for 10 sec, or so. Who really needs a notification in under 10 seconds anyway?

Your websockets solution may very well be the most practical solution. I haven't take the time to explore websockets route since the turn of the century (Could have been a different type of socket, some SOAP thing-a-majig, so I cannot make an educated comment on it.

"Be cool bro, Be cool bro, we're all on the same side here..."
 
Clearly, dchuk, you are mistaken. I was responding to the question posed by mattseh about a cron job.

I appreciate the explanation. I personally know mattseh isn't that familiar with cron jobs and needed the clarification.
 
Thanks for all the replies, I got it working. If you want to check out the system on the site, shoot me a PM and I'll send you the link.