• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 672
  • Last Modified:

Comet for xmlhttprequest (XHR) streaming and a JSON explanation

Although I have used AJAX my Comet (general word for technology allowing web server push) theory is very weak.

When using XHR object can you use multiple instances on the same connection (I imagine yes like websockets) and if you can how is this possible on one connection and one port, is it multiplexing?

How does XHR streaming work and could I have a very simple bit of AJAX code to see.

JSON explanation i.e. compared to XML and how has it anything to do with being an alternative to comet as I thought it was just like XML - a way to describe data?

  • 2
1 Solution
greetings  Johny_Brav0, , , I have only experimented with a PHP as XMLHttpRequest long polling, sometimes referred to as a "Comet" ajax Chat setup.  As far as I have seen the Comet ajax is used only for an online Chat application. You do not mention anything about a "Chat" setup that you are trying to do, if you are not doing a Chat, I doubt that you need to consider a long polling ajax setup like "Comet".

you ask - "multiple instances on the same connection  is it multiplexing?"

Ajax is usually set up as a non- synchronous javascript "event " or "Callback", when you send the ajax request to the server, the javascript does NOT wait for a responce, but continues to the next line of javascript code. The XMLHttpRequest object in javascript will use the event or Callback function that you have set for that object, when the server responds to the ajax request, code in the Callback function is executed, this is all done in your browser, and it is possible to have more than one ajax request active, however because of the current speed of the internet, this ususlly does not happen, I am not sure, I have not tried it, but I believe that the javascript does only ONE callback function at a time, is the server responds with a second request, while one in in the callback function, the first one has to finish before the second one is started, I do not think of this as "multiplexing".

I do not think that PHP is a good way to have Large Chat sites with long polling ajax setups, facebook tried to do this in PHP but had to set up a chat only server to use a JAVA language, it is well documented, you can do a web search about it if you want to know more.

For the most part, to have a Comet setup with PHP you will have a non-visible IFrame, that has a special php page in it that stays open (receptive to further output from the server) all of the time, this is not so easy in PHP as most PHP pages have a 30 second or shorter timeout or Maximum execution time. This frame page gets data sent to it only when there is a "change" of the chat data on the server side, it causes the parent page javascript to send an ajax request for the new data, this is a simplification of the process, but I hope it give you the idea.

XML and JSON  are to different ways to "Package" together many Data parts or segments, into one "Text" based file or memory block (data segment). So to me JSON is just like XML , using a different "Format" to identify and separate the parts and pices to place in them.  I almost never use either on for Ajax, and use the PHP array implode( ) and the -
var aArray = ajaxObj.responseText.split("\n\r");
in javascript.

I hope this gives you some Idea of what you ask about, , ask questions if you need more information.
Johny_Brav0Author Commented:
So would synchronous AJAX option be suitable for long polling for something like chat - guess it would?

I got a bit confused with multiplexing - got the wrong end of the stick in the lecture!

Last related question: is the following understanding correct?
Usually a TCP connection requires a socket i.e. ip:port. Web sockets does not work like that, it uses one port (80) for many connections, but this is multiplexed...

Sorry, but I do not begin to understand what your meaning is, or what you want clarification for - when you say - "So would synchronous AJAX option be suitable for long polling for something like chat - guess it would?"

You can set an ajax to "Wait" for the server response before going to the next line of javascript, ( I guess as termed "synchronous AJAX") however, if the server stays active, as in a long polling set up, then you do NOT get any response data to use until the server sends the "finished" data transfer sign, again I have simplified the methodology for clarity, but I would not think that this is used for the long polling ajax, but I will admit that I have never tried it, so I'm in theory mode, you can actually try it for your self and see what kind of results you get in the "Wait" for response setting.

I might ask you - WHY are you interested in Ajax Comet, long polling exchange? ? What do you hope to do with it ? ?

On your last inquiry about multiplexed TCP connection, I did not understand your concerns or point of reference? I went to Wikipedia and looked up multiplexing, to make sure I had a correct reference for your question. It is true that a Web server uses the port address of 80 for many connections from the web interface to browsers, there's no question about that, that's just how it is.

And Wikipedia  has this = "multiplexing (also known as muxing) is a method by which multiple analog message signals or digital data streams are combined into one signal over a shared medium."

So the short answer for your last question is = NO

as far as I know, each data "socket" connection is a separate connection with only ONE data stream being sent to the receiver (usually a web browser), I am not certain from experimentation, but I believe your browser only does one data transfer at a time, I.E.. .  image data transfers are separate transfers from HTML data transfers, but are not done at the same time, but in a sequence, one after the next.

You seem to not understand the difference of a server PORT connection data transfer, and multiple data streams ,being combined and streamed as a multiplexed data transfer as an asynchronous mode time-domain multiplexing which resembles time-division multiplexing.

I really do not understand what any of this has to do with PHP and ajax? ? so sorry about my mis understanding?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now