Skip to content

Instantly share code, notes, and snippets.

@igm
Created May 21, 2013 12:28
Show Gist options
  • Save igm/5619428 to your computer and use it in GitHub Desktop.
Save igm/5619428 to your computer and use it in GitHub Desktop.
Request-reply broker in Go
// Simple request-reply broker
//
// Author: Brendan Mc.
// Requires: http://github.com/alecthomas/gozmq
package main
import (
zmq "github.com/alecthomas/gozmq"
)
func main() {
context, _ := zmq.NewContext()
defer context.Close()
frontend, _ := context.NewSocket(zmq.ROUTER)
backend, _ := context.NewSocket(zmq.DEALER)
defer frontend.Close()
defer backend.Close()
frontend.Bind("tcp://*:5559")
backend.Bind("tcp://*:5560")
// Initialize poll set
toPoll := zmq.PollItems{
zmq.PollItem{Socket: frontend, Events: zmq.POLLIN},
zmq.PollItem{Socket: backend, Events: zmq.POLLIN},
}
for {
_, _ = zmq.Poll(toPoll, -1)
switch {
case toPoll[0].REvents&zmq.POLLIN != 0:
message, _ := toPoll[0].Socket.Recv(0)
backend.Send(message, 0)
case toPoll[1].REvents&zmq.POLLIN != 0:
message, _ := toPoll[1].Socket.Recv(0)
frontend.Send(message, 0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment