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.)
Who is Participating?
GaryConnect With a Mentor Commented:
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
That will give you compatibility behind each option and what is involved.
deleydAuthor Commented:
Any full working examples I can use as a starting point?
Ray PaseurCommented:
What is the remote device?
deleydAuthor Commented:
"Long Pole". There's the secret phrase that gives me the answers I seek. Thank you!
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.

All Courses

From novice to tech pro — start learning today.