3

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.

Samir
  • 269
  • 3
  • 6
  • 12
  • lots of discussions already on this topic: http://stackoverflow.com/search?q=ajax+push+poll check it! – Arfeen Dec 16 '11 at 13:20
  • I think your looking at it from the wrong perspective. Concertrate on "How get I get the client browser to recieve regular updates". The client can ask for an update on regular intervals, and the server gives the new information (if there is any). Most people use JSON, add a timestamp to the client request, and respond with "You don't need an update" or "Yes you have an update, heres the new info" – Abe Petrillo Dec 16 '11 at 13:22
  • @abe - any good examples using JSON ? – Samir Dec 16 '11 at 13:28
  • Are you familiar with jQuery and javascript? If so its just an ajax request called by setTimeout. I don't have time right now but I could probably set one up with jsfiddle as an example – Abe Petrillo Dec 16 '11 at 13:37
  • yea sure plzz ill be greateful .thanks – Samir Dec 16 '11 at 13:40

2 Answers2

2

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).

Community
  • 1
  • 1
leggetter
  • 14,640
  • 1
  • 50
  • 58
  • oh ok. can i know what language you use on your "Pusher". im talking about the language on the server side. – Samir Dec 16 '11 at 15:57
  • Node, Java, Groovy/Grails, Clojure, Python, VB.NET, C#, **PHP**, Ruby, Perl, ColdFusion - basically any language than can make a HTTP call to the Pusher RESTful API. Links to the Pusher server libraries here: http://pusher.com/docs/rest_libraries – leggetter Dec 16 '11 at 16:03
  • oh ok kool ! so how do i use your "pusher" and what else do i have to learn to use ur service ? – Samir Dec 16 '11 at 16:09
  • The tutorial I linked to in the answer is a good starting point along with the [quickstart guide](http://pusher.com/docs/quickstart). The [main docs page](http://pusher.com/docs) is also a good starting point. If you have further questions about the Pusher service I'd suggest you ask them on the [Pusher support forum](http://pusher.tenderapp.com). – leggetter Dec 16 '11 at 20:07
1

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

DarkDevine
  • 1,009
  • 1
  • 8
  • 12
  • flash is something not used much nowadays. so i would rather go for a javascript function . – Samir Dec 16 '11 at 13:30
  • Still it's an approach you can use to solve the problem the author has, since flash won't be gone in the next 2 years. Also, Abe Petrillo, noone talked about using flash to request, I used flash to respond – DarkDevine Dec 16 '11 at 13:35
  • @TorbenK. - can you show an example of how to achieve this as your example seems to be a good walkaround for my work :) – Samir Dec 16 '11 at 15:53
  • I tried searching for my old codes, but I only found the AS file opening the socket sadly. It's just about a flash.net.XMLSocket connecting to the server that wants to send, let the flash listen on DataEvent.DATA and maybe on Event.CONNECT. You can use Flash to send and respond, but for sending you can also just use AJAX. It worked, but I never worked in out in detail, I guess much more stuff like authentication and session management is needed for your project – DarkDevine Dec 17 '11 at 13:44