notification system in PHP/jQuery - php

I want to program some sort of notification system. What would be the be the best way to achieve this?
By calling Ajax request call to database on page load? But the problem with this is that it only checks on page load. It would be better if it was even realtime, I guess that depends on the priority of the message. How would I go about on achieving this?
By using cookies?
I am using PHP and jquery.

Like you said, it depends on message priority.
If notifications are made upon the visitor performing an action, it would be easy to tie in session-based notifications.
If notifications are made upon other users performing an action (e.g. "John made an edit to this page. Click here to view."), you could use long-polling AJAX to wait for a notification.

You might wanna look into COMET programming.
This would most likely require you to implement this as a distributed solution depending on your traffic.
Basically you'll have a java script function that goes to a server to check for notifications and if it finds any, calls another script that fetches the notifications back to the client. This should happen every so often, and WILL create a higher traffic and leave more connections open.
Take a look at this thread: Using comet with PHP?

For something small, I would consider using setInterval in combination with jQuery .load to load JSON.

Well AJAX and Jquery can be used to develop basic notification system. I've implemented it completely here: http://www.cloudways.com/blog/real-time-php-notification-system/
You can follow it step by step and also scale it as per your requirements.

Related

How to implement a push notification system on a website in PHP?

I would like to ask if someone knows how can I notify a user that looks at a page of my website with a push notification (i.e. make my server notify the user that there's something to him).
I understand the polling mechanism (I can implement it through a simple loop with a setInterval() in Javascript and pass a callback that makes an async XMLHttpRequest or a getJSON), but what about the push mechanism?
I guess I need to make a sort of a call with the server that should tell the client that it has something for him??? Assuming that my website is in PHP, is there a way to make it?
Thanks for the attention!
The network topology usually does not allow real push notifications. Certainly a browser won't. What you can do is using a special kind of polling strategy that comes close: "long polls".
Basically these are ajax based poll requests that are not immediately answered by the server. The server only sends an answer when some event is available or a defined timeout is reached. In that case the poll will be instantiated again right away by the client. In the mean time the socket stays open, the request does not consume any resources. In effect this allows push notifications.
How do I implement basic "Long Polling"?
Also obviously google will spit out tons of hits if you search for "php long poll".
Take a look at WebSocket - wiki.
Currently WebSocket supported by all popular browsers. You can check it here.
For PHP there is a good solution - Ratchet (http://socketo.me/)

how to make a popup chat application without using ajax

I have made a dating website where I have use one to one chatting application like facebook. When one user send any message to another user it showing into their popup chat box, but I have done this using ajax. Which I have run in every interval using javascript setInterval function. But I think the process is not optimize one. I don't want to make unnecessary request to the server each time, rather it only trigger when there is some new message for that user. Is there any other way to do it or any other protocol which using by big site like facebook, gmail?
You could do this using WebSockets, but that requires both a server implementation and a web browser that supports it.
Another technique is to use Long Polling, but again, this requires work on both the client and the server. The advantage is that this is a cross browser compatible technique.
I agree with Josh that WebSockets would be worth looking into, however if you don't have access to the server you could use something like Firebase for the back end.
https://www.firebase.com/index.html
Read into Long Polling. It's what facebook uses. Basically your client makes one Ajax call and nothing gets returned until there is data to push to it. I'm pretty sure it requires some custom server configuration so if you're developing on shared hosting it isn't going to cut it. Long Polling would be the right, albeit, more complicated way of doing this if efficiency is what you want.
Server-Sent Events seems to be another option.
A chat example: http://motyar.blogspot.com.es/2012/01/simple-chat-application-with-html5.html
Documentation: https://developer.mozilla.org/en-US/docs/Server-sent_events

JavaScript checking for new content such as notifications

I'll try and explain this as best I can. But basically on a website when you have some dynamic content such as messages, notifications, etc... You would want this information to be presented to the user as soon as the message arrived. Example a user is sent a new message show show a bubble with the count in the header of your website.
You see this stuff on nearly every single website these days and I'm unsure as to the best way to implement such a feature.
My initial idea was to write some ajax call and then wrap this in a setInterval call so it's requested every 30 or so seconds. The obvious problem here is that a) you have to wait 30 seconds for the request and b) you might be requesting the server hundreds of times in a day with no actual new content to ever display.
My second thought was to use EventListeners as they seem for more ideal as from what I understand they only do a request when the server tells it something. But I'm not 100% sure how to build something like this. Can anyone whip up a quick example or point to something that would serve as the basis for something like this?
So to clarify what I want to do:
When something new happens like sending a message or creating a notification this will send an event that the event listener picks up and updates something on the page either showing a small popup with the info or updating a bubble e.g. Messages (2)
I'll be using CakePHP and jQuery to implement this. So anything specific to this would be awesome... Hopefully someone can help me out.
Essentially I'm asking about EventListener vs setInterval and how it would work with ajax calls...
As suggested by Alex Ball, you should research about COMET programming techniques, and also look at the answers for this question Simple comet example using php and jquery. There isn't really a "simple" answer to implement an effective technique to what you are asking for but the most effective one is using an iframe. Using an iframe will allow you to have a persistent connection with the server (PHP side). Here you will be able to check for new messages (query for new messages) and if there is any, return your data. Your ajax call (success function) will the process this data and then do another post back to the server. If there is none then you would have to loop.
Again, effective COMET Programming techniques are not simple to implement.
Here are some useful examples though:
http://www.webreference.com/programming/javascript/rg30/index.html ,
http://www.zeitoun.net/articles/comet_and_php/start , setInerval wouldn't be effective for chat, maybe notifications.
COMET techniques are not specific to CakePHP.

Ajax push system

In my own system, I have a PHP page which displays all the goods I have sold through my online webshops, from the latest transaction to the first.
I would like this page to automatically update, whenever people buy something from me.
I could make AJAX call every 5 seconds to check the orders in my databases, but that seems 1980?
or is it that way people do it?
How can I go about pushing a notification to my php page whenever my php newOrder() function (lets call it that) is called?
You can achieve push within PHP but it won't be the most efficient solution because to achieve push you need to maintain long running connections between your client and your server (HTTP or WebSocket connections).
See:
Long Polling/HTTP Streaming General Questions
phpwebsocket
php-websocket on github
Ratchet
how to implement comet in PHP - frequently linked to guide
General best practice when building a realtime infrastructure has been to decouple the push solution from your web application (Note: node.js and socket.io has changed this a bit, but personally I still think it should be decoupled). But, assuming that the latter is still the best solution you would need to write/host/install this push solution. Decoupling also means that the technology doesn't have to be PHP but you can access/use it from PHP. Maybe a bit of overkill? And especially if you don't have too many users on your site?
For simplicity I would recommend looking at using a 3rd party hosted service. I work for one such company called Pusher. Using a service such as ours lets you remove the need to install and maintain the realtime part of your application. It also makes it really easy to add the push functionality you are looking for. All you need to do is add a few lines of PHP code to your existing app to trigger the push notifications and add a few lines of JavaScript to your front-end.
Resources:
Most commonly use PHP library for this: https://github.com/pusher/pusher-php-server
Quickstart guide
If you'd like to investigate the alternatives or some of the technologies I've mentioned above I'm maintaining a list of realtime technologies which you might also be interested in.
You could simulate a push effect by doing the following (pseudo code!)
// This would go on for 60-90 seconds, unless there is new data
for($i = 1; $i <= 60; $i++) {
// Check in the database what the last timestamp is
// Compare this to the timestamp that has been posted by the AJAX-call
// If there is something new, show the data and then exit
// If not, sleep for 1-3 seconds
}
Your javascript:
function pollForNewProducts() {
// Check if there is a timestamp
// Make an AJAX-request to the script that has the first code in it
// If there is a response (JSON?) than evaluate that
// If not, then run the script again
pollForNewProducts();
}
It is a simple yet effective way to let the server do all the hard work, instead of timeouts on the client side which will cause the browser to eat memory.
More about this on:
Simple “Long Polling” example code?
Client notification, should I use an AJAX Push or Poll?
PHP or Javascript poll code
ExtJS 3.0: Ext.Direct with PHP: Polling:ok and Pushing ?
Comet Programming: Using Ajax to Simulate Server Push
Apart from the excellent suggestion about nodejs ..if you want to still use php to achieve this, you want to look for the COMET method, not ajax.
Howto with php here : http://www.zeitoun.net/articles/comet%5Fand%5Fphp/start

How to run my php code in every X minute?

i try to make a "status monitor" for our small network. After the page was load i make a ping for every IP which i addedd. Its, ok. But i would like to do this ping in every X minute, without reload my hole page.
I can make it if i reload the page with header refresh, but i would like to do this witout reload.
I think i have to do this with AJAX?, But i dont know how..
Thank you
I would strongly suggest you have a look at Nagios or something similar:
1) you don't need to have a web page constantly open to detect problems
2) it can automatically verify and escalate issues
3) there are lots of probes available out of the box which can be used to measure all sorts of things - not just ping times
4) responding to a ping is not the same thing as working
5) it automatically collates stats to identify patterns of issues
6) it also provides SLA type reporting
7) Nagios is simple enough that even I can understand it
8) its what I chose after a lot of work researching a replacement for a system similar to you are suggesting.
HTH
C.
If it is entire code of page i suggest setting up a cron job
and if you want to use ajax ( ie jquery ajax there is a plugin called jquery timer) use it send a ajax request to the page with code you want to run.
http://plugins.jquery.com/project/timers
check this out
I suggest you take a look at some of the "other-way-around" approaches, such as COMET, here is an interesting article covering basic usage with PHP.
This would put the implementation of "ping" in your server instead of the client.
You could for instance instead of setting a fixed interval push out updates at will. Meaning you would get almost realtime status notifications instead of the fixed interval updates.
In web development, Comet is a
neologism to describe a web
application model in which a long-held
HTTP request allows a web server to
push data to a browser, without the
browser explicitly requesting it.
Comet is an umbrella term for multiple
techniques for achieving this
interaction. All these methods rely on
features included by default in
browsers, such as JavaScript, rather
than on non-default plugins.
COMET (Wikipedia)
Why don't you try a cron?
I'm not sure exactly what you want to do here, but this quick tutorial shows you how to call a php file every second and update a dib block with the results. It is quick and simple using jquery.

Categories