Learn R Programming

nanonext (version 0.5.4)

subscribe: Subscribe Topic

Description

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

Usage

subscribe(con, topic = NULL)

# S3 method for nanoSocket subscribe(con, topic = NULL)

# S3 method for nanoContext subscribe(con, topic = NULL)

Value

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

Arguments

con

a Socket or Context using the 'sub' protocol.

topic

[default NULL] an atomic type or NULL. The default NULL subscribes to all topics.

Details

To use pub/sub the publisher must:

  • specify mode = 'raw' when sending.

  • ensure the sent vector starts with the topic.

The subscriber should then receive specifying the correct mode.

Examples

Run this code
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, "examples will also be received", mode = "raw")
recv(sub, "character")
send(pub, c("other", "this other topic will not be received"), mode = "raw")
recv(sub, "character")

subscribe(sub, 2)
send(pub, c(2, 10, 10, 20), mode = "raw")
recv(sub, "double", keep.raw = FALSE)

close(pub)
close(sub)

Run the code above in your browser using DataLab