Nodejs and socket.io (Incorrect 'Sec-WebSocket-Accept') [Solved]

Dear forum users,

I really enjoy working with the arduino yun.

Still I have a question regarding the use of nodejs and socket.io. I followed the instructions of: Time to Expand your Yún disk space and install Node.js | Arduino Blog
and nodjs is working perfect.

Unfortunately, not every Node.js module is “pure javascript”, so some modules won’t work on the Yún. This also counts for socket.io. In the same blog is stated that you can use opkg install node-socket.io.

Also this is working, but can somebody explain how I can use this now for nodejs?

Best regards,
Martijn

Hello Feedback
indeed not every module is pure javascript: if you issue a

opkg list | grep node

you'll find we created packages for both socket.io and socket.io-client

Hello Federico,

Thanks for the fast response. It's working!

I'm used to that the modules are installed in the "node_modules" directory. "opkg install node-socket.io" installs the library global so that I can use them from anywhere. I was confused by that.

Please note that after a long wait socket.io 1.0 is now available. Would be nice if this will come available for the yun ;).

Again thanks for the support!

Unfortunately it is not working 100% as it should. Socket.io is running, but a fallback is made from websockets to xhr-polling.

The handshake for websocket failed. The Chrome log message: failed: Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value. After this message there is a fallback to xhr-polling. This problem does not occur when I run the nodejs script on windows or ubuntu. Maybe related to the "hashes problem" as mentioned: Time to Expand your Yún disk space and install Node.js | Arduino Blog

Does anybody else experience this behavior and has a fix for this?

May be. Fortunately I've contacted user chiari.show and he kindly sent me his patch. I'm going to apply it in the coming days and should be ready by next week (if it works)

Fingers crossed. If needed I can always run tests for you. Just drop me a message.

I should be able to attach the ipk later today for manually installing it

Here is the nodejs test ipk. Upload it to your yun and install it with opkg install node*.ipk
I'm sorry I'm not currently able to test, so I'm not sure it will work.

node_v0.10.28-1_ar71xx.ipk (2.7 MB)

Your fast :wink:

When installing I get the following error:

Collected errors:

  • opkg_install_pkg: Package node md5sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.
  • opkg_install_cmd: Cannot install package node.

Good news. I've installed the .ipk package manually and it's working! Your help is much appreciated.

It's working means: node-socket.io with websockets. As socket.io uses node-ws, I assume that this should be fixed as well.

Yahoo! :slight_smile:
I'll run the build on the build server and release an update asap. Thank you very much!

It took us more than expected, but a newer fixed version of node is now available. If you've already installed it, just upgrade it with

opkg update
opkg upgrade node

(Hope I haven't mistyped the command)

I've tested and it's working. Thanks for your support.

I know this is probably a big ask, but I'll ask anyway :roll_eyes:
Having got a very usable interactive web/yun setup now, for the purpose of ATE, remote service etc, I still have a thirst / quest to get websockets up and running. My brain has got overloaded with frustration from research and trials that unfortunately match my limitations. I really want to 'own' the socket, not be part of a mystical wired loop.

I can see how the protocol functions etc... For me, I have seemed to want to chose the node, socket / ws route for some gut reasoning.
Would it be possible to have the details of how you instigate the web socket on the Yun, for someone of my limited capability.
I am more than happy to go forward with the learning curve from that point, ( it's the way I got to this point!).

All I can do is ask, and with respect. ( gradually understanding Linux and its power )

Hello Roffey,

I think it will be easier to use python, due to the reason that the bridge library is already available. But I can give you an example to work with nodejs and websockets on the yun.

Installing nodejs and node-websockets on your yun:

opkg update
opkg install node
opkg install node-ws

Yun side code:
Create a directory, eg. nodejs. And copy the following code in a file (eg. server.js):

var WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({port: 8080});
wss.on('connection', function(ws) {
    ws.on('message', function(message) {
        console.log('received: %s', message);
    });
    ws.send('something');
});

Client side code:
You can run websockets in a modern webbrowser but for now I give you an example for nodejs.
Install nodejs on your PC. Create a directory, execute: npm install ws. Copy the following file (eg. client.js):

var WebSocket = require('ws')
  , ws = new WebSocket('ws://x.x.x.x:8080'); // x.x.x.x yun ip address
ws.on('open', function() {
    ws.send('something');
});
ws.on('message', function(message) {
    console.log('received: %s', message);
});

Now run both scripts. On the yun side: node server.js. And on the client side: node client.js

Enjoy!

Feedback, I'm puzzled: why haven't you suggested socket.io? Isn't it less low level and with more documentation available?

Thanks for the 'feedback' here .... pun intended.

I will play with that, and also do as you suggest and look into python. So much to learn, too little time. Always the cry, but you have to pay your dues and start somewhere. It helps when the starting bit actually works :slight_smile:

@Federico,

To be honest, I started working with socket.io. I only found that the initiation time of socket.io on the yun takes some time 0,5-1 second. Probally due to the fact that socket.io is a frame work on top of WS. If websocket is not available in the webbrowser you have a fallback to another technique (flash, ajax long polling, etc...), this could cause some extra timing. I need a fast response time during startup. That's why my choice of WS instead of socket.io.

@Roffey,
Federico is correct when talking about the available documentation regarding documentation on WS. Socket.io is easier compared to WS. For socket.io, please find below example.

For socket.io please try:

opkg install node-socket.io

javascript file:

var http = require("http");
var url = require('url');
var fs = require('fs');
var io = require('socket.io');
 



var server = http.createServer(function(request, response){
    console.log('Connection');
    var path = url.parse(request.url).pathname;
	console.log(path);
	//response.writeHead(200, {'Content-Type': 'text/html'});
	if (path == '/socket.html') {
	fs.readFile(__dirname +'/socket.html', function(error, data){
                if (error){
                    response.writeHead(404);
                    response.write("opps this doesn't exist - 404");
					console.log(error);
                }
                else{
                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data, "utf8");
                }
    response.end();
	
	});

	}
	else {
	response.writeHead(200, {'Content-Type': 'text/html'});
    response.write('hello world');
    response.end();
	}
	});
 
server.listen(8001);
 
//socket.broadcast.to('roomName').emit('eventName', {data: 'tosend'}); 
var server_io = io.listen(server);


server_io.sockets.on('connection', function(socket){


    //send data to client
    setInterval(function(){
        socket.emit('date', {'date': new Date()});
		//socket.emit('date', {'date': 'hallo'});
    }, 10);
//    socket.emit('message', {'message': 'hello world'});
	socket.on('client_data', function(data){
    process.stdout.write(data.letter);
  });

});

html file:

<html>
<head>
  <script src="/socket.io/socket.io.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
</head>
<body>
  <script>
  var socket = io.connect();
  socket.on('date', function(data){
    $('#date').text(data.date);
  });
   
  $(document).ready(function(){
    $('#text').keypress(function(e){
      socket.emit('client_data', {'letter': String.fromCharCode(e.charCode)});
    });
  });
  </script>
  <div id="date"></div>
  <textarea id="text"></textarea>
</body>
</html>

Usage: x.x.x.x:8001/socket.html (x.x.x.x - ip of your Yun)

Thanks Feedback. I don't think I could have asked for more. A Bit of inspiration is always good to have.
Time to look and learn. Thanks again.

Feedback:
To be honest, I started working with socket.io. I only found that the initiation time of socket.io on the yun takes some time 0,5-1 second. Probally due to the fact that socket.io is a frame work on top of WS. If websocket is not available in the webbrowser you have a fallback to another technique (flash, ajax long polling, etc...), this could cause some extra timing. I need a fast response time during startup. That's why my choice of WS instead of socket.io.

Ah, good point