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

subscribe(con, topic = NULL)

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

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

## 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.

## Value

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

## 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

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")
#>  [1] 65 78 61 6d 70 6c 65 73 00 74 68 69 73 20 69 73 20 61 6e 20 65 78 61 6d 70
#> [26] 6c 65 00
recv(sub, "character")
#> $raw #> [1] 65 78 61 6d 70 6c 65 73 00 74 68 69 73 20 69 73 20 61 6e 20 65 78 61 6d 70 #> [26] 6c 65 00 #> #>$data
#> [1] "examples"           "this is an example"
#>
send(pub, "examples will also be received", mode = "raw")
#>  [1] 65 78 61 6d 70 6c 65 73 20 77 69 6c 6c 20 61 6c 73 6f 20 62 65 20 72 65 63
#> [26] 65 69 76 65 64 00
recv(sub, "character")
#> $raw #> [1] 65 78 61 6d 70 6c 65 73 20 77 69 6c 6c 20 61 6c 73 6f 20 62 65 20 72 65 63 #> [26] 65 69 76 65 64 00 #> #>$data
#> [1] "examples will also be received"
#>
send(pub, c("other", "this other topic will not be received"), mode = "raw")
#>  [1] 6f 74 68 65 72 00 74 68 69 73 20 6f 74 68 65 72 20 74 6f 70 69 63 20 77 69
#> [26] 6c 6c 20 6e 6f 74 20 62 65 20 72 65 63 65 69 76 65 64 00
recv(sub, "character")
#> Warning: 8 | Try again
#> 'errorValue' int 8

subscribe(sub, 2)
send(pub, c(2, 10, 10, 20), mode = "raw")
#>  [1] 00 00 00 00 00 00 00 40 00 00 00 00 00 00 24 40 00 00 00 00 00 00 24 40 00
#> [26] 00 00 00 00 00 34 40
recv(sub, "double", keep.raw = FALSE)
#> [1]  2 10 10 20

close(pub)
close(sub)