nn_pubsub.adoc 2.8 KB

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