Node.JS to OSC via UDP

    Sep 16 2012 | 4:37 pm
    This is driving me nuts!
    I installed Node.js and integrated
    In a file called "appudp.js", I have the following code:
    var dgram = require('dgram');
    var message = new Buffer("5656"); // Whatever the number could be...
    var client = dgram.createSocket("udp4");
    client.on("error", function (err) {
        console.log("Socket error: " + err);
    // At every second, send a message...
    client.send(message, 0, message.length, 1337, "", function(err, bytes) {
        console.log("err : " + err + " | bytes : " + bytes + " | Message : " + message);
    }, 1000);
    I go in command prompt, type "node app.js". I get:
    err: null | bytes: 4 | Message: 5656
    All good so far.
    In MaxMsp, I have this very simple patch:
    Yet, as you can see in the screen capture, it gives me the error :
    OSC Bad message name string: DataAfterAlignedString: Unreasonably long string Dropping entire message.
    Being somewhat new to MaxMsp, I end up being completely lost. Help?
    I'm thinking it has to do with the content of the Buffer object (has to be an integer?) and the encoding (ascii?), but I'm not completely sure.
    Thank you very much for your time!

    • Sep 17 2012 | 9:51 pm
      Check out the following projects that Florian Demmer made for demonstrating how to use node.js together with Max, and then passing on to a web browser:
    • Sep 19 2012 | 8:33 pm
      I recommend taking a look at the mxj objects net.tcp.recv, net.tcp.send, net.udp.recv, and net.udp.send. Attached is an example using the net.tcp object to send messages to/from node.
    • Sep 20 2012 | 3:50 pm
      Thank you very much rjungemann. I looked into the source files and the server communication worked.
      I was now wondering if it was ever possible to send a TCP message from a local webpage? I thought the code under "// this client only sends" would enable me to do so, but it doesn't seem to be the case. In fact, nothing in this section seems to be executed when I log on the page or 6000.
      Is it because I'm misunderstanding the principles of TCP?
      Thank you again.
    • Sep 21 2012 | 6:39 pm
      If you want to be able to visit a webpage and send messages, you will need to have some sort of client (such as websockets) running in the browser, which would communicate with a server in Node JS. From there, Node JS will have to connect to Max MSP. The reason for this is that the browser is in a security sandbox and is not able to act as a server. You'll need to have a middleman that sits and waits for messages from the browser and forward them on.
      Browsers cannot communicate natively over raw TCP. HTTP uses TCP but you can't script the browser to communicate over raw TCP. You'll have to use something like Socket.IO, websockets, long polling, or just HTTP requests (easy but slow).
      TCP is just a generic way for sending messages between a client and a server. Web servers do use TCP to send messages from the browser to the server, but just because something uses TCP doesn't mean it will automatically work in the browser.
      Here is a diagram of a possible implementation:
      Browser to Max: Browser (running Socket.IO client) ---> Node JS (Socket.IO server) ---> Node JS (TCP client) ---> Max MSP (mxj net.tcp.recv)
      Max to Browser: Max MSP (mxj net.tcp.send) ---> Node JS (TCP server) ---> Node JS (Socket.IO server) ---> Browser (running Socket.IO client)
      Flash understands TCP sockets, so you could probably cut out a step if you use Flash in the browser.
    • Sep 21 2012 | 9:16 pm
      My previous example merely showed how to connect Node JS to Max, meaning it would be good to make command-line utilities that could communicate with Max.
      I will try and expand the example with browser functionality when I have some time.
    • Sep 21 2012 | 10:13 pm
      Try the attached example. Simply open Max, then start tcp-io-node-browser.js. Navigate your browser to http://localhost:5999/index.html
      From Max, click "foo bar baz". It will send that to the browser. The browser will respond with "hello from browser".
      Obviously this is a basic example but shows a round trip from Max to the browser, and back.
    • Sep 22 2012 | 7:17 pm
      Here's a library where someone is using Socket.IO to communicate between the browser and Node, then using UDP to communicate between Node and Max.
    • Sep 24 2012 | 5:28 am
      Again, thank you very much rjungemann. I will try the given examples/project files very soon.
    • Mar 21 2013 | 9:07 am
      How did this turn out @Justin ?
    • Mar 21 2013 | 11:18 am
      an alternative way could be based on the original post, but replacing [udpreceive] with [sadam.udpReceiver]. In this case, one would simply get the message as a list of ascii byte codes.
      Hope that helps,
    • Jun 10 2014 | 11:14 pm
      To revive this thread, both of the git repos are gone. Is there a example of how to get this data into the browser? It looks like it was here at one point. That component is missing and would be really helpful.
    • Jun 11 2014 | 11:54 pm
      This repo which has a specific max example in the deprecated files works out of the box: