Dev, Manage, Deploy

...and things in between...

Posts tagged node.js

16 notes

Logging incoming udp-messages with node.js

Last week, I realized I needed a quick way to set up logging of incoming udp-messages. My first idea was using netcat which is kind of a swiss army knife when it comes to network data. It worked, but for small scale mostly.

$ nc -l -u -p 4711 » udp-stream.log

This makes netcat listen on port 4711 and append the output to the log file. However, after each connection, the daemon is closed and needs to be restarted. Not very practical. The demo scenario was going to include somewhere around 300-500 messages / minute. 

Node.js eventually caught my attention, since its mentioned in at least two articles on the frontpage of HN each day. I decided to try to set it up for logging udp-messages and append them to a file. Some googleing of it turned up node.js documentation with a really good example, http://nodejs.org/docs/v0.3.1/api/dgram.html#dgram.bind.

The code for it is available on github, its really simple, https://gist.github.com/1406283

Easily tested by using netcat for sending a udpmessage
$ echo “test”  | nc -u server.hostname 4711
Even though I copied and pasted most of the code, it was a nice experience, since javascript is more or less fluent, I wont hesitate to use node.js in the future. 

Filed under node.js udp network