1 min read
Next in trending

Punt

A tiny UDP messaging library for nodejs

Punt

A tiny UDP messaging library for nodejs


In preparation for moving some of our stat collection and tracing data to UDP I’ve created Punt, an extremely small (68 SLOC) and simple library which makes UDP messaging even simpler.


Protocol

The Punt protocol couldn’t be simpler either, the packet consists of a single byte for the message type, followed by the data itself. This small abstraction on top of node makes it more natural to work with UDP in JavaScript-land.

Server

When a Punt Server is bound “message” events are emitted, retaining the original message format, meaning if you send a Buffer, you receive a Buffer, if you send an Object, it is converted to JSON and you receive an Object.

var sock = punt.bind(‘udp://0.0.0.0:5000');
sock.on(‘message’, function(msg){
console.log(msg);
});

Client

Naturally one or more clients may connect to the server to send messages. The API is shallow, with a single .send() method:

var sock = punt.connect(‘udp://0.0.0.0:5000');
sock.send(‘hello world’);
sock.send(new Buffer(‘hello world’));
sock.send({ hello: ‘world’ });

End

Really I just wanted to try Medium for writing code articles. Hopefully others find this lib useful as well!