nanonext (version 0.13.5.2)

socket: Open Socket

Description

Open a Socket implementing 'protocol', and optionally dial (establish an outgoing connection) or listen (accept an incoming connection) at an address.

Usage

socket(
  protocol = c("bus", "pair", "push", "pull", "pub", "sub", "req", "rep", "surveyor",
    "respondent"),
  dial = NULL,
  listen = NULL,
  tls = NULL,
  autostart = TRUE,
  raw = FALSE
)

Value

A Socket (object of class 'nanoSocket' and 'nano').

Arguments

protocol

[default 'bus'] choose protocol - 'bus', 'pair', 'push', 'pull', 'pub', 'sub', 'req', 'rep', 'surveyor', or 'respondent' - see protocols.

dial

(optional) a URL to dial, specifying the transport and address as a character string e.g. 'inproc://anyvalue' or 'tcp://127.0.0.1:5555' (see transports).

listen

(optional) a URL to listen at, specifying the transport and address as a character string e.g. 'inproc://anyvalue' or 'tcp://127.0.0.1:5555' (see transports).

tls

[default NULL] for secure tls+tcp:// or wss:// connections only, provide a TLS configuration object created by tls_config.

autostart

[default TRUE] whether to start the dialer/listener. Set to FALSE if setting configuration options on the dialer/listener as it is not generally possible to change these once started. For dialers only: set to NA to start synchronously - this is less resilient if a connection is not immediately possible, but avoids subtle errors from attempting to use the socket before an asynchronous dial has completed.

raw

[default FALSE] whether to open raw mode sockets. Note: not for general use - do not enable unless you have a specific need (refer to NNG documentation).

Protocols

The following Scalability Protocols (communication patterns) are implemented:

  • Bus (mesh networks) - protocol: 'bus'

  • Pair (two-way radio) - protocol: 'pair'

  • Pipeline (one-way pipe) - protocol: 'push', 'pull'

  • Publisher/Subscriber (topics & broadcast) - protocol: 'pub', 'sub'

  • Request/Reply (RPC) - protocol: 'req', 'rep'

  • Survey (voting & service discovery) - protocol: 'surveyor', 'respondent'

Please see protocols for further documentation.

Transports

The following communications transports may be used:

  • Inproc (in-process) - url: 'inproc://'

  • IPC (inter-process communications) - url: 'ipc://' (or 'abstract://' on Linux)

  • TCP and TLS over TCP - url: 'tcp://' and 'tls+tcp://'

  • WebSocket and TLS over WebSocket - url: 'ws://' and 'wss://'

Please see transports for further documentation.

Details

NNG presents a socket view of networking. The sockets are constructed using protocol-specific functions, as a given socket implements precisely one protocol.

Each socket may be used to send and receive messages (if the protocol supports it, and implements the appropriate protocol semantics). For example, sub sockets automatically filter incoming messages to discard those for topics that have not been subscribed.

This function (optionally) binds a single Dialer and/or Listener to a Socket. More complex network topologies may be created by binding further Dialers/Listeners to the Socket as required using dial and listen.

New contexts may also be created using context if the protocol supports it.

For supported protocols, an integer file descriptor is appended as the attribute 'fd'. This will become readable when a message is available for receiving on the socket. Attempts should not be made to read or write to the returned file descriptor, but it is suitable for use with poll(), select(), or WSAPoll() on Windows, and similar functions.

Examples

Run this code
s <- socket(protocol = "req", listen = "inproc://nanosocket")
s
s1 <- socket(protocol = "rep", dial = "inproc://nanosocket")
s1

send(s, "hello world!")
recv(s1)

close(s1)
close(s)

Run the code above in your browser using DataLab