Comet for xmlhttprequest (XHR) streaming and a JSON explanation

Posted on 2011-10-19
Last Modified: 2012-05-12
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?

Question by:Johny_Brav0
    LVL 33

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

    Author Comment

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

    LVL 33

    Expert Comment

    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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Javascript: Crash Course

    Javascript is not only emerging as the most important language to know, it's flexibility is unique. With Javascript being deployed on the web, mobile and even the desktop this is the one language that all technologists need to know.

    Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System ( introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now