1NN_PUBSUB(7) nanomsg 1.1.5 NN_PUBSUB(7)
2
3
4
6 nn_pubsub - publish/subscribe scalability protocol
7
9 #include <nanomsg/nn.h>
10
11 #include <nanomsg/pubsub.h>
12
14 Broadcasts messages to multiple destinations.
15
16 Messages are sent from NN_PUB sockets and will only be received by
17 NN_SUB sockets that have subscribed to the matching topic. Topic is an
18 arbitrary sequence of bytes at the beginning of the message body. The
19 NN_SUB socket will determine whether a message should be delivered to
20 the user by comparing the subscribed topics (using NN_SUB_SUBSCRIBE on
21 a full SUB socket) to the bytes initial bytes in the incoming message,
22 up to the size of the topic.
23
24 nn_setsockopt (s, NN_SUB, NN_SUB_SUBSCRIBE, "Hello", 5);
25
26 Will match any message with intial 5 bytes being "Hello", for example,
27 message "Hello, World!" will match.
28
29 Topic with zero length matches any message.
30
31 If the socket is subscribed to multiple topics, message matching any of
32 them will be delivered to the user.
33
34 Since the filtering is performed on the Subscriber side, all the
35 messages from Publisher will be sent over the transport layer.
36
37 The entire message, including the topic, is delivered to the user.
38
39 Socket Types
40 NN_PUB
41 This socket is used to distribute messages to multiple
42 destinations. Receive operation is not defined.
43
44 NN_SUB
45 Receives messages from the publisher. Only messages that the socket
46 is subscribed to are received. When the socket is created there are
47 no subscriptions and thus no messages will be received. Send
48 operation is not defined on this socket.
49
50 Socket Options
51 NN_SUB_SUBSCRIBE
52 Defined on full SUB socket. Subscribes for a particular topic. Type
53 of the option is string. A single NN_SUB socket can handle multiple
54 subscriptions.
55
56 NN_SUB_UNSUBSCRIBE
57 Defined on full SUB socket. Unsubscribes from a particular topic.
58 Type of the option is string.
59
60 EXAMPLE
61 int pub = nn_socket (AF_SP, NN_PUB);
62 int sub = nn_socket (AF_SP, NN_SUB);
63 int nbytes;
64 void *buf = NULL;
65 char *addr = "inproc://example";
66
67 nn_setsockopt (sub, NN_SUB, NN_SUB_SUBSCRIBE, "foo", 3);
68 nn_setsockopt (sub, NN_SUB, NN_SUB_SUBSCRIBE, "bar", 3);
69
70 nn_bind(pub, addr);
71 nn_connect(sub, addr);
72
73 nbytes = nn_send (pub, "foo|Hello!", 10);
74 assert(nbytes == 10);
75 nbytes = nn_recv (sub, &buf, NN_MSG, 0);
76 assert (nbytes == 10);
77 nn_freemsg (buf);
78
79 nbytes = nn_send (pub, "baz|World!", 10);
80
81 /* Message is not delivered because if matches no subscription. */
82 nbytes = nn_recv(sub, &buf, NN_MSG, 0);
83
85 nn_bus(7) nn_reqrep(7) nn_pipeline(7) nn_survey(7) nn_pair(7)
86 nanomsg(7)
87
89 Martin Sustrik <sustrik@250bpm.com>
90
91
92
93 2020-08-24 NN_PUBSUB(7)