Dojox/Socket, Node.js, and Socket.io

Monday, Sep 16, 2013 10:57 pm
William Barnes

I’ve been playing around with node.js and I decided to try setting up a web socket connection. I’m using socket.io on the server side and Dojo Toolkit on the client side. Things on the server-side were straightforward. However, I decided to give dojox/socket a shot and got the following error:

-debug- destroying non-socket.io upgrade

As it turns out, dojox/socket is a fairly plain wrapper over the browser’s built-in Websocket object (with long-polling support added). Socket.io does rather a bit more and (rightly or wrongly) seems to prefer talking to itself. Luckily, if you’re not too picky, you can easily include the official (AMD-compatible) socket.io client:

  1. require([
  2. '../socket.io/socket.io.js'
  3. ], function(io) {
  4. var socket = io.connect('ws://localhost:8000');
  5. socket.send("Hiya server!");
  6. });

Note that the proper relative path to the socket.io.js file has to be specified (the proper version of this file is automatically served by socket.io through node.js).

  • http://ayottesoftware.com/ Richard Ayotte

    I was hoping that I wouldn’t have to do that but alas, looks like I will have to. Thanks for posting.