Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documentation about sending different messaging formats #74

Closed
dpastoor opened this issue Mar 12, 2015 · 3 comments
Closed

documentation about sending different messaging formats #74

dpastoor opened this issue Mar 12, 2015 · 3 comments

Comments

@dpastoor
Copy link

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!

@stevengj
Copy link
Contributor

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.

@dpastoor
Copy link
Author

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

@JamesWrigley
Copy link
Member

I'm gonna close this because I think it's decently documented in the docstring now: https://juliainterop.github.io/ZMQ.jl/latest/reference/#Sockets.send

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants