Skip to content

Instantly share code, notes, and snippets.

@jamescarr
Last active January 2, 2016 01:49
Show Gist options
  • Select an option

  • Save jamescarr/8233282 to your computer and use it in GitHub Desktop.

Select an option

Save jamescarr/8233282 to your computer and use it in GitHub Desktop.
zmq = require 'zmq'
address = 'inproc://foobar'
class Cycle
constructor: (@items) ->
next: ->
@items.push(item = @items.shift())
return item
channelA = zmq.socket 'router'
channelA.identity = 'channel-A'
channelA.on 'message', (envelope, message) ->
console.log "#{envelope} sent back #{message}"
channelB = zmq.socket 'router'
channelB.identity = 'channel-B'
channelB.on 'message', (envelope, number) ->
channelB.send [envelope, parseInt(number) + parseInt(number)]
channelC = zmq.socket 'router'
channelC.identity = 'channel-C'
channelC.on 'message', (envelope, number) ->
channelC.send [envelope, number * number]
cycle = new Cycle(['channel-B', 'channel-C'])
channelA.bind address, ->
channelC.connect address
channelB.connect address
num = 10;
setInterval(->
env = cycle.next()
console.log "sending #{num} to #{env}"
channelA.send [env, num++]
, 2000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment