Learn R Programming

nanonext (version 0.4.0)

subscribe: Subscribe Topic

Description

For a socket using the sub protocol in a publisher/subscriber pattern. Set a topic to subscribe to.

Usage

subscribe(socket, topic = NULL)

Arguments

socket

a Socket using the sub protocol.

topic

[default NULL] a topic (given as a character string). The default NULL subscribes to all topics.

Value

Invisibly, an integer exit code (zero on success).

Details

To use pub/sub the publisher must:

  • specify mode = 'raw' when sending to allow the topics to be recognised by the receiving party.

  • send a vector that separates the topic from the rest of the message e.g. send(socket, c("topic", "message"), mode = "raw") - this ensures that topic ends with the required nul byte for it to be recognised.

Examples

Run this code
# NOT RUN {
pub <- socket("pub", listen = "inproc://nanonext")
sub <- socket("sub", dial = "inproc://nanonext")

subscribe(sub, "examples")
send(pub, c("examples", "this is an example"), mode = "raw")
recv(sub, "character")
send(pub, c("other", "this other topic will not be received"), mode = "raw")
recv(sub, "character")

close(pub)
close(sub)

# }

Run the code above in your browser using DataLab