Node.JS to OSC via UDP

Justin's icon

This is driving me nuts!

I installed Node.js and integrated Socket.io.

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...

setInterval(function(){

client.send(message, 0, message.length, 1337, "127.0.0.1", 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: http://i.imgur.com/FQrvO.png

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!

Joshua Kit Clayton's icon

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:

rjungemann's icon

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.

4426.TCPIOwithNode.zip
zip
Justin's icon

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 127.0.0.1:6001 or 6000.

Is it because I'm misunderstanding the principles of TCP?

Thank you again.

rjungemann's icon

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.

rjungemann's icon

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.

rjungemann's icon

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.

rjungemann's icon

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.

Justin's icon

Again, thank you very much rjungemann. I will try the given examples/project files very soon.

timothybone's icon

How did this turn out @Justin ?

$Adam's icon

Hi,

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,
Ádám

Andrew Blanton's icon

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.

Andrew Blanton's icon

This repo which has a specific max example in the deprecated files works out of the box:

geofholbrook's icon

Ádám, five years later ... sadam.udpReceiver works for this application, where udpreceive doesn't. Thank you!