Created
September 10, 2012 19:22
-
-
Save stianeikeland/3693168 to your computer and use it in GitHub Desktop.
0MQ.. push/pull => pub/sub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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