“Real” Javascript Sockets!

I could not find a single way to have real sockets in Javascript. Google told me that there probably is no solution except embeding a java applet or an active-x component! So I thought why not using a little .swf file as a bridge from javascript to the socket functions of flash!
You can see the result here:
http://dev.dschini.org/socketjs/
This works pretty neat, fast and stable! The client connects to a patched ("\0\n") Unreal IRCD. Except connecting and joining a channel there is no much functionality, but it works pretty good! If you are interested please give me some feedback!

36 Comments

  1. Hannes 2006-06-14, 8:28 am

    Sehr. Coole. Sache.

  2. gast 2006-06-21, 12:44 pm

    under what license is it released?

  3. Bob 2006-06-22, 3:19 am

    Potentially, could this be used to set up a phishing site that sends out spam whenever a visitor, uhhh, visits? If so, there are some scary ramifications.

  4. Cypher 2006-06-22, 4:04 pm

    This “novelty” is nothing new.

    As far as I remember, it does not work under IE for Mac.

  5. Sean 2006-06-23, 1:34 pm

    Just what I was looking for. I’m developing a javascript browser based game and I wanted real server push.

  6. Chris 2006-07-17, 3:43 am

    This works great when connecting to a server on localhost, but when connecting to a server over the internet or even over LAN, I simply can’t get it to work.

    If anybody knows how to fix my problem, PLEASE help me. I’ve made an awesome online game with this but no players can connect! E-mail me: kaoskorruption at gmail.com. Thanks.

  7. Chris 2006-07-19, 7:52 pm

    I’d be interested in making it work with the new version of flash. I don’t know much about flash but I’ll learn a bit if its necessary. I’ve started to make a game with this and I need it to work with the new flash!

  8. Chris 2006-07-21, 6:35 am

    This works with the latest flash version in FireFox. It must only be IE that doesn’t work with the new version.

  9. sly 2006-07-23, 1:18 am

    SocketJS in Java here: http://sly.miniplaza.net/socketjs/

    :-)

  10. Manfred 2006-07-23, 7:58 pm

    hey sly,
    this is funny :)

  11. Kaos 2006-07-26, 8:25 pm

    For IE7 (and possibly previous versions of that piece of crap), SocketJS must send data between each time it receives. That’s stupid.

  12. Rui Ribeiro 2006-08-10, 10:37 pm

    Hi,

    This is a great idea. After some rough start I’ve finaly putted it to work with the server (only sending, not receiving yet).

    The problem seems to be that “socketjs” send’s a “” character after each send. The server software doesn’t like that…

    The server isn’t sending the “” character back and the “socketjs” doesn’t like that either..

    Does any one know if this “real sockets” have to send these annoying “” characters on both ways each send?

    I don’t have the “flash” editing software so I can’t really tweak anything… :-(

    Thanks to all, nice work.

  13. AkonD 2006-08-16, 7:04 am

    Hi

    Somebody could explain like patching UnrealiRCD so that it works with socketjs?

    Chris socketjs in java does not finish working to me, you can put to download java compiled pleas.

  14. Manfred 2006-08-16, 7:14 am

    Hi AkonD,
    you can download the patched server here:
    http://dev.dschini.org/Unreal3.2.zip
    After unziping you have to set chmod to some important files.
    – Manfred

  15. AkonD 2006-08-16, 7:23 am

    Hi

    I use another version of unrealircd(unrealircd uDB)

    I need single the file that there is to change

    thx Manfred

  16. Jon 2006-08-23, 3:07 am

    Outta curiosity, how does your library compare to Aflax?

    (I used that library to build my first javascript socket project, but I’m not sure how it compares. The approach is similar…

    … but presumably, yours would be a little better, if a person only wanted socket connections and nothing else.

    Thanks a lot for figuring this stuff out — it’s a clever solution which really makes a lot of things possible.

  17. James 2006-09-20, 6:42 pm

    Hi,
    I just started using javascript for client-server programming and found socketJS is very convenient and powerful.

    Everything works for me if I put it on the local disk of the browser, like file:///C:/test/index.html. It can send and receive data from a remote server.

    But it fails when I have it (html, swf) on a web server, like http://192.168.1.203/test/. In this case, I can get the page right and swf is downloaded to the browser. I have “SocketConnect(’192.168.1.201′,6688)” in the html. But when I click on the “Connect”, it tries to connect to 192.168.1.201:80 instead. The destination port is changed to 80.

    Any idea?

    Thanks for sharing.

  18. DJSnoopy 2006-09-26, 10:10 pm

    After hours of testing and scripting i got this result:

    - Got 2 recode the linefeeds in Flash for communication with Pear.Net.Server (PHP)

    - If message is to large Flash pumped out his socket-message as multiple Packets.
    this blew up the Pear-Server :-(

    I will be happy 4 all tips to resolve this!

  19. jan 2006-09-27, 2:49 am

    Great piece of software, I got 2 questions:

    1) what is the license type? GPL, LGPL, BSD, Public Domain?

    2) What about Flash v8? I read it is not compatible with Flash v8? That means it is useless because everybody will have Flash v8 in 6 months.

  20. Ken Fehling 2006-10-24, 1:28 am

    For all who are having problems when moving your project from localhost to a host on the web, make sure you add www before the hostname. That might help. Aside from that, pretty smooth working with this. Really good stuff.

  21. Ken Fehling 2006-10-24, 1:48 am

    Actually, you need to use www if the url in the address window uses www, if not you can’t. So I wrote a small script that checks window.location.href and returns the appropriate host to connect to. I even made it return “localhost” for cases when I’m testing offline.

  22. Ken Fehling 2006-10-24, 1:55 am

    function getHostName() {
    var url = window.location.href;
    var index = url.indexOf(”.com”);
    if(index == -1) {
    return “localhost”;
    }
    return url.substring(7, index 4);
    }

  23. Ken Fehling 2006-10-24, 1:57 am

    at the very end that should be index 4 (index plus 4)

  24. Ken Fehling 2006-10-24, 10:15 am

    var i = textInput.value;
    if (i !== “”) {
    i = i.replace(/\\/g, “\\\\”);
    i = i.replace(/’/g, “'”);
    i = i.replace(//g, “>”);
    window.document.socket.SetVariable(”data”, i);
    window.document.socket.TCallLabel(”/”, “send”);
    textInput.value = “”;
    }

  25. Pascal Beyeler 2006-11-1, 9:14 pm

    Hi

    I’m really confused. I can’t get it to work properly. I uploaded this script 1:1 to my web server. But it always says “connection failed”. Local it’s working but not on my webserver. I’d like to connect to your dev.dschini.org:6667.

    if you have some hints .. send an email thx

  26. Manfred 2006-11-2, 9:14 am

    Hi Pascal,

    That does not work because the socket of the flash-movie within the html page needs to be in the same network domain like the sockets is.

    Best Regards
    Manfred

  27. amnon 2007-10-2, 12:13 am

    JavaScript sockets and any other OS functionality you might need:

    http://www.jnext.org

    cross platform, cross browser, small footprint and free

  28. Manfred Weber 2007-10-2, 12:19 am

    Hi,

    looks interesting. I will have a deeper look at it.

    cheers
    Manfred

  29. Gotisch 2007-11-19, 4:08 am

    this wont work for cross domain servers unless you have the crossdomain.xml on the other server google for crossdomain.xml.

  30. webrunner 2007-12-12, 7:42 pm

    Is there an equivalent to this that uses Flash 9’s new Socket object? There’s something I’m working on but there’s no way I can easily work around the termination issue of XMLSocket

  31. Ken Fehling 2007-12-25, 7:54 pm

    I’m having trouble testing SocketJS in firefox on localhost. I think firefox put some security against this. It still works in Internet Explorer. I’m considering making a version of this using OpenLaszlo or Flex to test further.

  32. Ken Fehling 2007-12-28, 3:04 am

    I got Firefox 3 Beta and it works on localhost like a dream.

  33. Manfred Weber 2007-12-28, 11:40 am

    hey Ken. I had a look at your website an especially the chat system. looks very impressive! wow!

  34. Aidamina 2008-05-11, 2:28 am

    Inspired by your code i decided to write my own implementation.

    see: http://jsocket.googlecode.com

  35. Matt Haynes 2008-07-29, 4:31 pm

    Excellent work!

    I too have an implementation on the go:

    see: socketBridge

  36. sechole 2009-02-4, 12:17 pm

    There is a good reason why don’t you have sockets in JS/untrusted java applets. Having them is a huge sechole, thats why.

    Imagine that the user enters a site then in the next second his/her browser joins a DoS attack without any notice.

Add a Comment