[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Comet for xmlhttprequest (XHR) streaming and a JSON explanation

Posted on 2011-10-19
3
Medium Priority
?
639 Views
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?

Cheers
0
Comment
Question by:Johny_Brav0
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Slick812 earned 2000 total points
ID: 37002157
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.
0
 

Author Comment

by:Johny_Brav0
ID: 37003528
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...



0
 
LVL 34

Expert Comment

by:Slick812
ID: 37004102
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?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

872 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