Tuesday, July 1, 2008

TCP in the browser?

Allen brought to my attention a blog post by Michael Carter about how Orbited now offers a JavaScript API for making TCP connections. I'm not sure how useful this is right now (and I actually mean that I'm not sure, not that I think it's useless and am trying to avoid saying so), though I can see how if, at some future date it's possible to make TCP connections without going through a proxy, apps written this way will suddenly make a lot of sense and be easy to port from apps already based on an API like this.

Anyhow, I thought this would be simple to implement with Athena, but I wanted to see how simple. Five minutes later I had this untested code:

# athenatcp.py
from twisted.internet.protocol import ClientCreator, Protocol
from twisted.internet import reactor

from nevow.athena import LiveElement, expose

class TCPElement(LiveElement):
jsClass = 'Network.TCP'

def connect(self, host, port):
def connected(proto):
self.proto = proto
self.proto.dataReceived = self.dataReceived
self.proto.connectionLost = self.connectionLost
cc = ClientCreator(reactor, Protocol)
d = cc.connectTCP(host, port)
return d

def write(self, bytes):

def dataReceived(self, data):
self.callRemote('dataReceived', data)

def connectionLost(self, reason):
self.callRemote('connectionLost', reason.getErrorMessage())

// athenatcp.js

// import Nevow.Athena

Network.TCP = Nevow.Athena.Widget.subclass('Network.TCP');
function connect(self, host, port) {
return self.callRemote('connect', host, port);

function dataReceived(self, bytes) {
// override this

function connectionLost(self, reason) {
// override this

It'd probably be better if connect were actually a factory function returning TCP instances, but the general idea would be the same. Anyone think this is a really great idea? Anyone want to turn this spike into a real library for Athena?


  1. I've seen the idea before but never an implementation. I can think of some test applications, like a browser-based telnet client. Thing is, very quickly anything useful gets to be too much load on the browser-side. Telnet? SSH, please, and all the key algorithms and encryption in JavaScript? Who wants to write that?

    This should have been done years ago, before everything started getting pushed through HTTP anyway.

  2. SSH is unnecessary with SSL

  3. Do you even use SSH? SSL+Telnet < SSH

  4. That's great!
    If several servers all supported the same API, we could use the exact same clients in JavaScript.

    It looks like it would just take a slight change to your code for people writing clients like this to use it. (We wanted Orbited's TCPSocket API API to look just like the WebSocket API in HTML 5.)

    Anyway, it would be really awesome if Athena provided a socket for JavaScript! I'll happily discuss the advantages with you any time if you are still undecided. :-)

    -Frank Salim

  5. Browsers do expose some crypto APIs implemented in native code for JavaScript writers. More could be added.