Sockets and socket.io

Posted on
  • I use socket.io to enable browser to browser communication in a number of projects.

    For example, a client running in Chrome might include:

    	var socket = io();
    	socket.on('matrix', function(packet) {
    		// Process packet
    	});
        socket.emit('register',{'artworkid': artworkid, 'artworktype': artworktype});
    
    

    And on the node.js server. This would "bounce" the messages between clients:

    io.on('connection', function(socket) {
      console.log('Connected');
      socket.on('disconnect', function() {
        console.log('Disconnected');
    	console.log("Clients: "+Object.keys(io.sockets.clients));
      });
    
      socket.on('register', function(msg) {
      	socket.artworkid = msg.artworkid;
      	socket.artworktype = msg.artworktype;
      	console.log("Registered: "+msg.artworkid+" "+msg.artworktype);
      });
    
      socket.on('matrix', function(msg) {
      	socket.broadcast.emit("matrix", msg);
        //console.log("matrix: "+msg);
      });
    });
    

    How do I get a client running on the Espruino to join in? I don't think there is a socket.io library for Espruino so do I need to use ordinary sockets to do the same job? Would I need to take messages received in socket.io and resend them to the Esprunio and visa versa? Are there some good examples of Espruino to node.js communication via sockets that I should be looking at?

    Thanks,

    Sean

  • Yes, the socket.io libs are just too big for Espruino (even the slim one seems to be 50kB!). I can't find any documentation at all on the actual low-level protocol either - so I think your best bet would be to have Espruino connect straight to a Node.js server.

    Espruino's socket implementation is designed to be a cut-down version of Node's, so if you take a look at http://www.espruino.com/Internet#socketsĀ­ then you should be able to run the 'server' code on the server, and the 'client' code on Espruino - basically without modification.

    Your other option I guess is to run MQTT on your server, and to bridge it with socket.io. It might be an easier way to ensure everything stays as individual packets, since the socket's just a stream of bytes, and there is no guarantee that sending abcdef won't result in two callbacks, one for abc and one for def.

  • Thanks Gordon,

    I was going to look at MQTT next - it sounds like the way to go.

    Cheers,

    Sean

  • The idea of routing/bridging packets between socket.io (for web browsers) and MQTT (for Espruino) via node.js works really well. It provides a very nice way for my web-based artworks to communicate with microcontroller-based ones. I think I could probably move completely over to MQTT (on the browser too) but for now I'll stick with what I have produced.

    My next step is to sort out the lost packets when using neopixels (with that test version of the library) and get DMX light control working.

    Still loving the platform!

    Sean

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Sockets and socket.io

Posted by Avatar for seanclark @seanclark

Actions