ZMQ.jl icon indicating copy to clipboard operation
ZMQ.jl copied to clipboard

documentation about sending different messaging formats

Open dpastoor opened this issue 9 years ago • 2 comments

Pardon if this is a ridiculous question, I am new to passing data around, but I didn't see any examples or documentation regarding the technique for sending various types of data.

For julia, is the process always to just accept the generic message then convert it to an IO stream then to the final format? I got the following to work for passing a basic string, and was wondering if the process would be the same/similar for different data structures being passed around?

server:

using ZMQ

ctx=Context(1)
s1=Socket(ctx, REP)

ZMQ.bind(s1, "tcp://127.0.0.1:5555")
println("listening....")

while true
    msg = ZMQ.recv(s1)
    print("GOT", msg) # should print "test request" in Uint8 
    ZMQ.send(s1, Message("test response"))
end

client

using ZMQ
ctx=Context(1)
s2=Socket(ctx, REQ)
ZMQ.connect(s2, "tcp://127.0.0.1:5555")
ZMQ.send(s2, Message("test request"))
msgin = ZMQ.recv(s2)
out = convert(IOStream, msgin)
@show msgin 
string = takebuf_string(out))

However, if I instead wanted to send a json object to perform a calculation on, as example in zmq with python to send a json array of numbers and returned the summed values I would do the following

server:

import zmq
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://127.0.0.1:5000")

while True:
    msg = socket.recv_json()
    print("GOT", msg['numbers'])
    socket.send_json(sum(msg['numbers']))

client:

import zmq
import json

array = '{"numbers": [1, 2, 3]}'
data = json.loads(array)
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://127.0.0.1:5000")
socket.send_json(data)
print("Sending", data)
msg_in = socket.recv()
print(msg_in)

Can someone advise (even via pseudocode) how this would be accomplished via julia?

Thanks!

dpastoor avatar Mar 12 '15 01:03 dpastoor

You can convert directly to a string with bytestring (assuming UTF8-encoded data); you don't need to convert it to a stream. See e.g. how IJulia receives IPython messages.

stevengj avatar Mar 25 '15 18:03 stevengj

ah perfect - this is exactly what I was looking for -thanks!

dpastoor avatar Mar 25 '15 19:03 dpastoor