10

I am looking to add comet long polling functionality to my site by using ajax and jquery as I am not familiar with the javascript prototype framework. However, when I search for this, I always seem to end up with tutorial which use the prototype framework. Does anyone know of any comprehensive tutorial for comet long polling based on jquery and ajax?

oshirowanen
  • 15,297
  • 82
  • 198
  • 350

5 Answers5

26

Here's a Comet tutorial using jQuery http://screenr.com/SNH
Code used in this tutorial

  • 4
    The loop which it constantly runs in that tutorial, is that not server heavy? – oshirowanen Oct 15 '10 at 10:47
  • 1
    That was fricking awesome. I understand how to do basic comet now...sweet :) – crosenblum Jan 07 '11 at 14:39
  • @oshirowanen - On your server's script, for example PHP, the comet would be repeating a loop (either endless or timed) but within that loop, using PHP's usleep() or sleep() actually separates/forks server processes. Of course if every time the loop runs you're performing heavy processes, that would apply server load accordingly :) – Prof Nov 21 '12 at 22:23
  • Hello JARVIS. Just want to confirm that example you gave is actually a comet or its turn way round? Means using time-stamp logic its implemented. Please make me clear. Thank you. – Rajan Rawal Oct 24 '13 at 18:21
  • 3
    Thanks for the code, it looks awesome, but lemme ask one thing: what happens if I close the browser, I suspect that the while loop still will work, and suppose the user closes the browser and after that during the next hour or so nothing changes in the server, so, the while loop will meaninglessly work during that hour, won't it ? Thanks – dav Dec 10 '13 at 12:51
  • 1
    Is this anything more then repeated ajax calls to the server? I see in my firebug, just call after call.... – bart2puck Feb 02 '15 at 18:48
  • It is just a basic example how comet can be implemented. If you want to achieve true comet, please do some research, I am sure there are plenty of libraries/tutorials are available. –  Feb 04 '15 at 22:16
  • 3
    Link is broken. – Brian Stork Feb 01 '19 at 21:32
  • this is why you don't post links as only answer on so.... – Sojtin Mar 14 '19 at 08:17
2

Cometd has bindings for both the jQuery and Dojo JavaScript libraries. I don't know how comprehensive it is, but it has documentation here: http://cometd.org/documentation/cometd-javascript

Ash
  • 9,296
  • 2
  • 24
  • 32
0

You can force the use of long polling with this

var cometd = $.cometd;
cometd.websocketEnabled = false;

Hope it helps

Pablo Johnson
  • 1,044
  • 15
  • 21
0

Comet and Long polling imply writing part of your code in client side and part on server side. You would need in server CometD or Ape to been able to connect to a Comet server, i find that installing them are usually a pain in the ass.

The option that i have found way more easy is to have Node.js with a "comet" module. Faye(also available for ruby) is a long pole pub sub server quite usefull for chats and Socket Io is a great way to have a page updated constantly to sockets(websockets with flash fallback).

Faye Site:http://faye.jcoglan.com/ Socket Io:http://socket.io/

Siedrix
  • 641
  • 4
  • 10
0

I have a very simple example here that can get you started with comet. It covers compiling Nginx with the NHPM module and includes code for simple publisher/subscriber roles in jQuery, PHP, and Bash.

http://blog.jamieisaacs.com/2010/08/27/comet-with-nginx-and-jquery/

A working example (simple chat) can be found here:
http://cheetah.jamieisaacs.com/

Jamie
  • 811
  • 9
  • 13