Node.JS to OSC via UDP

Sep 16, 2012 at 4:37pm

Node.JS to OSC via UDP

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!

#64276
Sep 17, 2012 at 9:51pm

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:

https://github.com/fde31/node-MaxComm

https://github.com/fde31/MaxClientJS

#231766
Sep 19, 2012 at 8:33pm

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.

Attachments:
  1. TCPIOwithNode.zip
#231767
Sep 20, 2012 at 3:50pm

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.

#231768
Sep 21, 2012 at 6:39pm

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.

#231769
Sep 21, 2012 at 9:16pm

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.

#231770
Sep 21, 2012 at 10:13pm

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.

#231771
Sep 22, 2012 at 7:17pm

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.

https://github.com/fde31/MaxClientJS

#231772
Sep 24, 2012 at 5:28am

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

#231773
Mar 21, 2013 at 9:07am

How did this turn out @Justin ?

#231774
Mar 21, 2013 at 11:18am

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

#231775

You must be logged in to reply to this topic.