Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

XmlHttpRequest, Ajax, need example of communicating with remote device

I've read dozens of articles but still haven't found a working example. I want a web page to communicate with a remote device.

HTML5 Websockets will eventually be the easy solution. But not everything supports that yet.

XmlHttpRequest has the problem of refusing to communicate with a device not in the same domain.

JSONP sounds like the remote device needs to support it. (I think the remote device is supposed to send JavaScript.)

PHP has stream_socket_client which I've actually had success with. However, if the communication gets messed up it doesn't seem to recover and I have to restart the web server to get it to unhang. (Remote device responds with a TCP/IP "Reset", indicating it isn't happy and wants to start over, but it doesn't seem to be happening on the PHP side.)

So, anyone know of any good examples sample working code that actually works and gets around all these little details that make the system not work?

Solution can be Ajax, XmlHttpRequest, PHP sockets, which I've only read I should use stream_socket_client and/or stream_socket_server instead.

Oh that reminds me, in addition to being able to initiate a connection with the remote device and talking to it, and also listening to it to receive any data it might want to send at any time; I also need the remote device to be able to initiate a connection to a server to send an email message or SMS text message whenever it wants to. (That's actually a separate issue I believe.)
Avatar of Gary
Gary
Flag of Ireland image

One of the simpler ways to around the same origin policy is to use a proxy page.
This means instead of calling the remote domain from the browser you call a proxy page on your own server
This proxy page makes the call to the remote server and returns the results to the browser.

JSONP is the other way to do this and will normally work in all cases. jQuery makes this simple by handling the callback for you.

For the rest of your question then sockets is the way to go and makes it a lot easier to maintain persistent connections
There are other alternatives but we are now getting into a very complicated area and cannot be answered in a simple comment
A bit of reading
http://dsheiko.com/weblog/websockets-vs-sse-vs-long-polling
That will give you compatibility behind each option and what is involved.
Avatar of deleyd

ASKER

Any full working examples I can use as a starting point?
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What is the remote device?
Avatar of deleyd

ASKER

"Long Pole". There's the secret phrase that gives me the answers I seek. Thank you!