Server side notifications to frontend for PHP - php

I don't know how it's called exactly, but I need that server send notices to frontend, that allow do not refresh page. Such as at the stackoverflow, that show serverside changes like new messages without page refresh. I can solve my problem with always monitoring the server side backend every 5 seconds for example, but it's a very load the server. Help me to solve this problem for PHP, maybe exists some third-party libs for this and tell me how they are called?

You can either go for Web Sockets or if you can use HTML5, use Server Side Events to get informed about certain events by remote party.

If you want to avoid polling the server for updates, what you're looking for is WebSockets.

Related

Best way for real-time HTML/AJAX/PHP chat

I have to create a little AJAX chat in my web application and I'm dealing with problem of real-time communication between javascript client and PHP server. I want my js client to be able to catch new messages from the server as quick as possible. My first idea was to create AJAX request for example each 5 sec. to see whether there are new messages. However, I'm not sure what happens if my application use for example 1000 people, it must be huge load to Apache httpd. I also know about technique called 'long-polling' request, but when I tried that locally on my server, I've completely shooted down my Apache (I've read sth about problems with apache and long-polling). The next way I know about is WebSocket. However, is it true that I have to be able to open port on webserver to use it? Because on regular web hosting, I thing it's not possible and I cant change any Apache/PHP settings on my hosting. Do you have any suggestions how to solve it?
Thank you and excuse my english please, I'm not native.
It is real time comments system. You can use it as chat
Real time chat
You should use php comet and push from the server instead of doing requests.
Check out this question also

Openfire XMPP - pipe a user's messages to a url

I'm fairly new to XMPP and Openfire and am trying to get my head round the standard way of doing things.
I'd ideally like to send any messages received by a specific XMPP user, via POST, to a URL where a PHP script can then process the message. This would all happen on my local server / local Apache installation.
I've seen that with BOSH I can make a persistent connection listening for XMPP messages through PHP, however perhaps I'm mistaken, but it doesn't sound like a very stable way of doing things - I worry that I'd either end up with multiple persistent connections or my BOSH PHP script would time out and I wont realise. I also ideally wouldn't always have the browser open and running this script!
Can anyone point me in a sensible direction to start me off? Many thanks
Are you trying to create some chat using php, or want to intercept messages to certain user to do some custom work ?
Have you taken look at javascript for chat http://candy-chat.github.com/candy/
Maybe you will find there something that might help you move in right direction.

How to update a webpage (only) when something changed on the server side

I have a simple php page that displays data from a mysql database. I want it to update itself automatically when data gets changed on the server. (I don't want to update the page periodically at fix time intervals.) I guess I need the technology behind FB chat box or omegle. But I don't know how to implement it on php and mysql. I would be grateful if you could help me. Thanks.
You would need to look into WebSockets or a Comet server (which uses long-polling techniques) to accomplish a push system. Alternatively, instead of using push-like notification, you could make frequent polls to the server with nothing more than a request identifier and a timestamp, let the server decide if there is anything new since the last poll, and serve up the data if there is.
you can implement Comet technology which is opposite of Ajax. JavaScript Dojo Toolkit can be useful to handle this method well.
Dojo WebSocket
http://dojotoolkit.org/features/1.6/dojo-websocket
http://cometd.org/
"Comet is 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."
http://en.wikipedia.org/wiki/Comet_(programming))

PHP / MySQL auto update clients

I'm writing a website using PHP / MySQL. The website should allow users browsers to be automatically updated when a table is updated in a database (which is caused by someone inserting something though the PHP site). Basically, if the state of the table changes other users on the website should immediately see the changes.
I'm relatively new to PHP / AJAX and other web technologies. The only way I can think of doing this is to have the clients manually recheck the database every second or two via a timer (but this seems like a lot of wasted bandwidth). Is there some way to have the users automatically notified when the database changes?
Thank you!
You already had the solution in mind. You can use ajax to automatically check for changes.
It's not possible for the server to contact you when something has changed. It doesn't have to be bandwidth intensive when you lower the retrieval intervals. You can have Ajax check for a blank page with as little as possible text. That way the bandwidth usage wont be too bad.
Facebook does it this way too.
1) AJAX Polling.
As you say, use ajax to call the PHP on the server side to get
updates.
2) HTML5 Websockets.
Websockets are a new feature of HTML5 which allow the browser to keep a bi-directional TCP connection open to the server, where data can be transmitted both ways without polling. Currently this isn't as widely supported as ajax polling is.
3) Java Applet/ActiveX control.
Generally the most undesirable solution because of the user having to install third party software.

How to Use Long Polling or Ajax Push in your Website

I want to know how to use ajax push . i have learnt from various Web articles that Ajax push can be obtained by using few programmes like COMET, APE (AJAX PUSH ENGINE) etc.... But i want to know whether there is a simpler way of using it and what language is used to implement ajax push. because in the articles which i have seen. they are using java. which i did not learn :( so i would like to know whether there is something like : a javascript in your server which sets an interval to a particular item and then if any changes found then echo it out using php. ? please help me out for this . its been a week now i tried to achieve this. i tried to use normal ajax and php by using intervals but not able to get the result. Thank you. P.S : Please show me an easy way of using it with an example or something.
If you want to use PHP as your backend technology then it's going to be an uphill struggle. Have a read through this question on concurrency - How to implement event listening in PHP for more information.
The simplest solution for PHP developers in my opinion is to use a hosted realtime service like Pusher - who I work for. This means you don't need to worry about the installation or maintenance of your realtime web infrastructure and most importantly you don't need to worry about your server handling persistent concurrent connections. You use the Pusher JavaScript library connects to Pusher from the web browser, maintains a persistent connection and receives any updates pushed to it and the Pusher REST API to publish data from your PHP app, through Pusher, to the connected clients.
There's a getting started with Pusher guide on Nettuts+ which has been very popular and is a good starting point for anybody using PHP.
If you really want to host your own realtime infrastructure on PHP (don't say I haven't warned you) then you can look at How to implement PHP with Comet and PHP WebSockets (there's also a project on github with recent activity called php-websocket-server).
I used a very simple approach based on flash some time ago
I included a little 1px*1px transparent flash on my page that opened a socket to the server my AJAX sends requests to. The server receives the AJAX request and responds on the flash socket
The flash just opens a javascript: url that calls an onreceive event handler, so you won't open a new page but run the javascript on your current page

Categories