0

I want to do is PUSH through jQuery when my mongodb adds an entry. I saw a lot of jQuery plugins out there, but they do LONG POLLING not PUSH.

I am very keen to know how this is happening here (if you will open two different windows and try to chat you will know)

This guy wrote this sample code.

He is using prototype.js, and I saw in backend code, there is nothing special. Just MySQL select statements, but whenever the MySQL database changes prototype comet object responses to that.

Can we do something similar in jQuery? I belive this is not polling but PUSH through prototype.js.
Or is it like prototype is connected to socket? Sorry, I have not much idea about what sockets are, but what I really want to do is PUSH through jQuery when my mongodb adds an entry.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Swati Sharma
  • 1,025
  • 2
  • 7
  • 8
  • I fail to see how a client requesting for changes is *push* and not polling – Alexander Jul 26 '12 at 19:56
  • Client isn't asking. php responses to that , when mysql entry is there. If you will open console window and type something in the input box , you will see, its xhr request, but on PUSH. How? – Swati Sharma Jul 26 '12 at 19:57
  • The client asks by making the initial connection to the Comet server. The Comet server then pushes whatever it's told to push to whatever clients are connected. While apps for Comet are written in JavaScript, jQuery isn't an appropriate tool for this as it's intended for DOM manipulation, not what you're trying to do. – WWW Jul 26 '12 at 19:59
  • Yes correct, but there is no comet server!! Read his code at http://bit.ly/QeNEMK . How come? – Swati Sharma Jul 26 '12 at 19:59
  • It's long-polling. Just because he calls his object 'Comet' doesn't mean that's what he's using. So to correct my earlier comments - there is no Comet server involved. I made assumptions that I found were erroneous when I actually looked at the code. – WWW Jul 26 '12 at 20:05
  • well.. if its LONG polling, then why an XHR request happens only when there is some data at server?? I see a lot of jquery long polling plugins, they keep on sending request after n number of seconds. but here there is no frequent request check. XHR Request only happens when there is some data on server – Swati Sharma Jul 26 '12 at 20:07
  • No, the XHR request happens when the page loads and then immediately **AFTER data has been received**. – WWW Jul 26 '12 at 20:08
  • @Crontab , how can we do this in jQuery then? Or native Javascript? – Swati Sharma Jul 26 '12 at 20:09
  • 1
    Umm, you have quite a bit more research to do (on your own) before you tackle that problem if you don't fully understand how the demo app you linked works. Sorry. – WWW Jul 26 '12 at 20:14

1 Answers1

-1

Let's settle this once and for all. Since I'm not a prototype user I got to read the documentation to come up with this.

You can access the underlying XMLHttpRequestRedirected this way from your JavaScript console.

$ comet.ajax.transport.readyState
1

readyState is 1 which stands for OPENED.

Want to test this through? Override the XMLHttpRequestRedirected.onreadystatechanged avoiding it to reconnect (and stay open waiting.)

$ comet.ajax.transport.onreadystatechange = function(){
  console.log("Don't reconnect");
}; 
$ comet.ajax.transport.abort();

You'll see the Don't reconnect message and the long-polling will be stopped.

Now try sending messages. Nothing will happen.

Alexander
  • 23,432
  • 11
  • 63
  • 73