views:

88

answers:

4

I have a browser based app that needs to communicate with another service running on the client machine via a socket connection from the browser using JavaScript.

I need to post and parse XML back and forth on the socket.

I am unable to go down the flash path as the cross domain security is a barrier, ie the service running on the socket is not able to be modified to support Flash's crossdomain security.

What are my options for a pure JS based solution?

A: 

Javascript will not allow you to create a socket connection to the client. It would violate the same origin policy. If you could somehow save an applet/swf to the local machine you could serve it up as file:/// and it could communicate to localhost (maybe! not tested).

Byron Whitlock
Hmmm, yes, I see. No Adobe's Flash security wont allow socket connections on non hosted swfs (i.e. from file://) but I can embed a swf in the page to lookafter socket comms. I was wanting to investigate any pure options that does not require flash tho. Websockets look interesting, in supported browsers.http://dev.w3.org/html5/websockets/Also investigating a custom plugin/extension option.All paths lead to some other plugin/widget bridging it seems :(
Vaughan
A: 

Maybe creating a proxy to go in front of this unmodifiable socket server could open up some options for you. You could then use something like flash, or you could just not use sockets.

chris
A: 

Your options for socket based interaction is limited to plugins that support such live functionality. The options generally break down as follows Flash, Java and Silverlight. All of which aside from Java, if I recall correctly, will have similar policy requirements.

If you control your own server, you could create a socket service to proxy the request to the final destination. Or, depending on the interaction, you can use standard Ajax-style requests and have the socket interaction on your server-side code. If you don't need a persistent connection, having the socket interaction via the server is your best bet.

Tracker1
+1  A: 

You've got two major problems here:

  1. It's difficult in javascript to access non HTTP resources,
  2. It's difficult in javascript to access resources not loaded from the same server.

There are exceptions to both of these, but the conjunction of exceptions available might not exactly match with what you need. Here are some possibilities:

  1. Some sort of proxy on your own server that connects back to the machine with the XML service on behalf of your web app.

  2. If you can control the client machine somewhat you can run a server on it that can embed the XML in a JSONP formatted http response, you can access by adding simple script tags, and send messages the other way by using a script tag to request a url with your data encoded into it.

  3. If when you say 'socket' you're referring to an HTTP connection, then there are a number of options, one is to add a Access-Control-Allow-Origin header to the HTTP, then you can do gets and posts using normal XMLHttpRequests in recent browsers.

Adam