if (FALSE) {
### Using request-reply pattern.
### At the server, run next in background or the other window.
library(pbdZMQ, quietly = TRUE)
context <- zmq.ctx.new()
responder <- zmq.socket(context, ZMQ.ST()$REP)
zmq.bind(responder, "tcp://*:5555")
for(i.res in 1:5){
buf <- zmq.recv(responder, 10L)
cat(buf$buf, "\n")
Sys.sleep(0.5)
zmq.send(responder, "World")
}
zmq.close(responder)
zmq.ctx.destroy(context)
### At a client, run next in foreground.
library(pbdZMQ, quietly = TRUE)
context <- zmq.ctx.new()
requester <- zmq.socket(context, ZMQ.ST()$REQ)
zmq.connect(requester, "tcp://localhost:5555")
for(i.req in 1:5){
cat("Sending Hello ", i.req, "\n")
zmq.send(requester, "Hello")
buf <- zmq.recv(requester, 10L)
cat("Received World ", i.req, "\n")
}
zmq.close(requester)
zmq.ctx.destroy(context)
}
Run the code above in your browser using DataLab