JavaScript is specifically forbidden from making “raw socket connections” that would make this possible. JavaScript can only access HTTP, FTP, or (rarely) some other resource-fetch-only protocols.
(Note that the streaming sockets features of WebSockets are based initially upon doing an HTTP handshake, and then going to the “almost raw” sockets mode; but this was designed to intentionally prevent connecting to arbitrary services. One of the fears was that, e.g. a spammer could hijack web browsers to connect to SMTP servers and relay mail using random users' machines as springboards.)
However, Firefox extensions can access the XPCOM layer (XUL) — through the JSLib system. Specifically, the socket class is found here: http://www.mozdev.org/source/browse/jslib/libraries/network/socket.js?annotate=1.6
It looks like a pretty good asynchronous interface, similar to the select
/read
loop that you might use in a single-threaded C server implementation. (Remember, JavaScript is all single-threaded…)
Alternatively, although slightly less portably (in today's world), you might be able to download a Java applet with your code in it, but:
- applets are forbidden from connecting to arbitrary systems, without special permissions, which I don't know how to / if XPI's can provide;
- many Firefox users don't have Java installed; it's not even available on some platforms that Firefox runs on.