Socket Functions {pbdZMQ} | R Documentation

## Socket Functions

### Description

Socket functions

### Usage

    zmq.socket(ctx, type = .pbd_env$ZMQ.ST$REP)

    zmq.close(socket)

    zmq.bind(socket, endpoint, MC = .pbd_env$ZMQ.MC)

    zmq.connect(socket, endpoint, MC = .pbd_env$ZMQ.MC)

    zmq.disconnect(socket, endpoint, MC = .pbd_env$ZMQ.MC)

    zmq.setsockopt(socket, option.name, option.value, MC = .pbd_env$ZMQ.MC)

    zmq.getsockopt(socket, option.name, option.value, MC = .pbd_env$ZMQ.MC)

### Arguments

`ctx` | a ZMQ context
`type` | a socket type
`socket` | a ZMQ socket
`endpoint` | a ZMQ socket endpoint
`MC` | a message control, see `ZMQ.MC()` for details
`option.name` | an option name to the socket
`option.value` | an option value to the option name

### Details

`zmq.socket()` initials a ZMQ socket given a ZMQ context `ctx` and a socket `type`. See `ZMQ.ST()` for the possible values of `type`. ZMQ defines several patterns for the socket type and utilize them to communicate in different ways including request-reply, publish-subscribe, pipeline, exclusive pair, and naive patterns.

`zmq.close()` destroys the ZMQ socket.

`zmq.bind()` binds the socket to a local endpoint and then accepts incoming connections on that endpoint. See `endpoint` next for details.

`zmq.connect()` connects the socket to a remote endpoint and then accepts outgoing connections on that endpoint. See `endpoint` next for details.

`endpoint` is a string consisting of a transport :// followed by an address. The transport specifies the underlying protocol to use. The address specifies the transport-specific address to bind to. pbdZMQ/ZMQ provides the following transports:

Transport | Usage
`tcp` | unicast transport using TCP
`ipc` | local inter-process communication transport
`inproc` | local in-process (inter-thread) communication transport
`pgm,epgm` | reliable multicast transport using PGM

*** warning: `epgm` is not turned on by default in the pbdZMQ's internal ZeroMQ library.
*** warning: `ipc` is not supported in Windows system.

`zmq.setsockopt()` is to set/change socket options.

`zmq.getsockopt()` is to get socket options and returns `option.value`.

### Value

`zmq.socket()` returns an R external pointer (`socket`) generated by ZMQ C API pointing to a socket if successful, otherwise returns an R `NULL` and sets `errno` to the error value, see ZeroMQ manual for details.

`zmq.close()` destroys the socket reference/pointer (`socket`) and returns 0 if successful, otherwise returns -1 and sets `errno` to the error value, see ZeroMQ manual for details.

`zmq.bind()` binds the socket to specific `endpoint` and returns 0 if successful, otherwise returns -1 and sets `errno` to the error value, see ZeroMQ manual for details.

`zmq.connect()` connects the socket to specific `endpoint` and returns 0 if successful, otherwise returns -1 and sets `errno` to the error value, see ZeroMQ manual for details.

`zmq.setsockopt()` sets/changes the socket option and returns 0 if successful, otherwise returns -1 and sets `errno` to the error value, see ZeroMQ manual for details.

`zmq.getsockopt()` returns the value of socket option, see ZeroMQ manual for details.

### Author(s)

Wei-Chen Chen wccsnow@gmail.com.

### 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.ctx.new()`, `zmq.ctx.destroy()`.

### Examples

    ## Not run:
    ### Using request-reply pattern.

    ### At the server, run next in background or the other windows.
    library(pbdZMQ, quietly = TRUE)

    context <- zmq.ctx.new()
    responder <- zmq.socket(context, .pbd_env$ZMQ.ST$REP)
    zmq.bind(responder, "tcp://*:5555")
    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")
    zmq.close(requester)
    zmq.ctx.destroy(context)

    ## End(Not run)

[Package pbdZMQ version 0.2-4 Index]