Skip to content

Instantly share code, notes, and snippets.

@stianeikeland
Created September 10, 2012 19:22
Show Gist options
  • Save stianeikeland/3693168 to your computer and use it in GitHub Desktop.
Save stianeikeland/3693168 to your computer and use it in GitHub Desktop.
0MQ.. push/pull => pub/sub
# Router / Central hub for home automation.
# Receives events and distributes via pub/sub
zmq = require 'zmq'
inputPort = 'tcp://*:8888'
outputPort = 'tcp://*:9999'
# Pull socket for incoming (push/pull), pub for outgoing (pub/sub)
input = zmq.socket 'pull'
output = zmq.socket 'pub'
output.bind outputPort, (err) ->
throw err if err
console.log "Publisher listening on #{outputPort}"
input.bind inputPort, (err) ->
throw err if err
console.log "Pull listening on #{inputPort}"
input.on 'message', (data) ->
try
jdata = JSON.parse data
if not jdata.event?
jdata.event = "unknown"
strData = JSON.stringify jdata
console.log "Repeating package of type: #{jdata.event} >> #{strData}"
output.send "#{jdata.event} #{strData}"
catch error
console.log "Invalid data: #{error}"
process.on 'SIGINT', () ->
input.close()
output.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment