Skip to content

Instantly share code, notes, and snippets.

@randrews
Created August 12, 2011 15:25
Show Gist options
  • Save randrews/1142283 to your computer and use it in GitHub Desktop.
Save randrews/1142283 to your computer and use it in GitHub Desktop.
Lua ZMQ examples
require 'zmq'
ctx = zmq.init(1)
socket = ctx:socket(zmq.REQ)
socket:connect 'tcp://localhost:4568'
k = 0
while k < 10 do
msg = string.format('ping %d', k)
socket:send(msg)
print(socket:recv())
k = k + 1
os.execute 'sleep 1'
end
socket:close()
ctx:term()
require 'zmq'
ctx = zmq.init(1)
socket = ctx:socket(zmq.PUB)
socket:bind 'tcp://*:4568'
while true do
zip = math.random(89999) + 10000
temp = math.random(70) + 50 -- temp range 50 .. 120; welcome to Texas
socket:send(string.format('%d %d', zip, temp))
end
socket:close()
ctx:term()
require 'zmq'
ctx = zmq.init(1)
socket = ctx:socket(zmq.REP)
socket:bind 'tcp://*:4568'
while true do
s = socket:recv()
print(string.format('Echoing %q', s))
socket:send(s)
end
socket:close()
ctx:term()
require 'zmq'
ctx = zmq.init(1)
socket = ctx:socket(zmq.SUB)
socket:setopt(zmq.SUBSCRIBE, '78701')
socket:connect 'tcp://localhost:4568'
while true do
print(socket:recv())
end
socket:close()
ctx:term()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment