pbdZMQ (version 0.2-1)

Send Receive Functions: Send Receive Functions

Description

Send and receive functions

Usage

zmq.send(socket, buf, flags = .pbd_env$ZMQ.SR$BLOCK)

zmq.recv(socket, len = 1024L, flags = .pbd_env$ZMQ.SR$BLOCK, buf.type = c("char", "raw"))

Arguments

socket
a ZMQ socket
buf
a buffer to be sent
flags
a flag for the method using by zmq_send and zmq_recv
len
a length of buffer to be received, default 1024 bytes
buf.type
buffer type to be received

Value

  • zmq.send() returns number of bytes (invisible) in the sent message if successful, otherwise returns -1 (invisible) and sets errno to the error value, see ZeroMQ manual for details.

    zmq.recv() returns a list (ret) containing the received buffer ret$buf and the length of received buffer (ret$len which is less or equal to the input len) if successful, otherwise returns -1 and sets errno to the error value, see ZeroMQ manual for details.

Details

zmq.send() is a high level R function calling ZMQ C API zmq_send() sending buf out.

zmq.recv() is a high level R function calling ZMQ C API zmq_recv() receiving buffers of length len according to the buf.type.

flags see ZMQ.SR() for detail options of send and receive functions.

buf.type currently supports char and raw which are both in R object format.

References

ZeroMQ/4.1.0 API Reference: http://api.zeromq.org/4-1:_start

Programming with Big Data in R Website: http://r-pbd.org/

See Also

zmq.msg.send(), zmq.msg.recv().

Examples

Run this code
### 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, .pbd_env$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, .pbd_env$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 DataCamp Workspace