Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : Best way to push data from server to clients.....?

In my project I need to push some data from server to the client like facebook does, when some updates are performed in the clients profile like somebody writes something in the wall of the client, Facebook automatically push it to the client side.

Currently I am using AJAX to continuously check the DB for the updates. That is not a good practice.

I don't want to depended on the client side for the updates. I want that the server should automatically push the data to the client side when some updates are performed related to the client.

Please suggest some url or sample code to do so......

Thanks in advance.....

like image 852
Pushpendra Avatar asked Apr 22 '11 08:04

Pushpendra


People also ask

How send data from server to client in PHP?

function send(){ var formdata = new FormData(), id = document. getElementById('id'). value; formdata. append('id', id); var xhr = (window.

Can server push data to client?

It needs to be clear in mind that in HTTP protocol, the server can't send any data without getting a request from the client which is actually a case of PUSH. So server can't PUSH data over HTTP to the client if it's not requested.

Can Web server send data without a request from client?

You can use web-sockets which are two way communication between client and server that remain open and allow the server to send without request from the client. However web-sockets are new and browser support is only for the most current browsers.


2 Answers

You should take a look at the APE Project, which supports multiple variations that are as close to Push as you will get, like long-polling, XHRStreaming and WebSockets.

  • http://www.ape-project.org/
  • http://www.ape-project.org/wiki/index.php/Tutorial:Use_different_transport_method_(JSONP,_XHRStreaming)#Long_Polling

edit Doing this in just PHP is not the best choice, as it takes up a lot of resources to keep all those connections alive.

like image 176
CharlesLeaf Avatar answered Oct 08 '22 01:10

CharlesLeaf


you can use node.js: http://nodejs.org/

like image 1
sdolgy Avatar answered Oct 08 '22 01:10

sdolgy