Sending UDP from a package


Hi folks!

I’m trying to build a workaround for live-coding like stuff for my art project, and I want Atom to send a message over UDP whenever it saves a file with specific extension. I made a simple extension with using dgram package, but unfortunately it doesn’t send any messages, nor does it return any error.

dgram = require 'dgram'
{CompositeDisposable} = require 'atom'

module.exports = Phrase =
  client: null
  activate: (state) ->
    @subscriptions = new CompositeDisposable
    @subscriptions.add atom.commands.add 'atom-workspace', 'phrase:toggle': => @toggle()
    @client = dgram.createSocket('udp4')

  deactivate: ->
  serialize: ->

  toggle: ->
    console.log 'toggle'
    msg = new Buffer('/bang')
    @client.send(msg, 0, msg.length, 10110, '', (err) -> console.log(err, msg))

The console looks like this:

toggle 0 Buffer {0: 47, 1: 98, 2: 97, 3: 110, 4: 103, length: 5, parent: Object, asciiSlice: function, base64Slice: function, binarySlice: function…}

Any thoughts on the issue?



I doubt this has anything to do with Atom. dgram just runs inside node. You should check with the repo for dgram.

Edit: Are you sending this to know when the file you are saving is updated? There are many ways to just watch for the file to change, which seems easier to me.


BTW, where is the console.log call that prints this?


Hi Mark,

Thank you for your response.

I’m doing this workaround for Max [1], a visual programming language for real-time multimedia, and it doesn’t have a ready-made solution for file watching, afaik. My friend created a mini-DSL inside that language and we want to edit the small scripts in an external editor.

It’s in the anonymous function, the last argument of the @client.send method. The strange thing here is that the err variable in the anonymous function contains zero, which means, I suppose, “no error”.

Btw, maybe you can suggest some other library for UDP? I’m totally newbie with nodejs and don’t know the mainstream stuff here.



It can listen for a UDP message but it can’t watch for files? What is the low-level language you are using to listen for the UDP?

I’ve never used UDP in node but I have used TCP/IP. I didn’t use a package. The built-in node API is easy to use. I assume it is easy for UDP also.


Exactly :slight_smile: UDP (with OSC protocol) is a de-facto standard for art apps to communicate with each other.

Ok, thanks, Mark, I think it’s worth to check more of node’s APIs. dgram was the first think I’ve found, but probably there is more.


I just looked up dgram. I didn’t realize it was built into Node. So that is definitely the way to go. Have you found any tutorials for it?


OK, I checked the code of dgram. It turned out, that you need to bind the socket to a port before sending datagrams [1]. Maybe I don’t understand something, but isn’t it illogical to force port binding? With UDP we can just send a packet and forget about it, so no need to listen on port.

There is my modified activate function:

  activate: (state) ->
    @subscriptions = new CompositeDisposable
    @subscriptions.add atom.commands.add 'atom-workspace', 'phrase:toggle': => @toggle()
    @client = dgram.createSocket('udp4')
    @client.bind(20220 )


The node docs say you don’t have to …

“If the socket has not been previously bound with a call to bind, it gets assigned a random port number and is bound to the “all interfaces” address (‘’ for udp4 sockets, ‘::0’ for udp6 sockets.)”

Oh well …