[nanomsg] publish/subscribe keys

  • From: "John Carneiro" <johncarneiro@xxxxxxxxxxx>
  • To: <nanomsg@xxxxxxxxxxxxx>
  • Date: Wed, 13 Aug 2014 16:47:57 -0400

Hello,

Described in this document is a simple pub/sub example, however neither
it nor the nanomsg documentation describes how to use publish/subscribe keys or if they exist:
http://tim.dysinger.net/posts/2013-09-16-getting-started-with-nanomsg.html

It seems like this line from the example:
nn_setsockopt (sock, NN_SUB, NN_SUB_SUBSCRIBE, "", 0)

could easily be instead something like this to subscribe to a subscription type of "sub1":
const char* subType = "sub1";
nn_setsockopt (sock, NN_SUB, NN_SUB_SUBSCRIBE, subType, strlen(subType) + 1)
and I would assume you could read this string on the publishing server like this
char subType[100];
getsockopt(sock, NN_SOL_SOCKET, NN_PROTOCOL, &subType, sizeof(subType));

See below code modified from the original example to get my general direction.

Is this the intent of pub/sub ?
If so, are there API calls that I'm missing or done incorrectly?
If not, what protocol or method would be better ?

Thanks

-

#include <assert.h>
 #include <stdio.h>
 #include "../src/nn.h"
 #include "../src/pubsub.h"
 #include "string.h"

 #define SERVER "server"
 #define CLIENT "client"

 char *date ()
 {
       time_t raw = time (&raw);
       struct tm *info = localtime (&raw);
       char *text = asctime (info);
       text[strlen(text)-1] = '\0'; // remove '\n'
       return text;
 }

 int server (const char *url)
 {
       int sock = nn_socket (AF_SP, NN_PUB);
       assert (sock >= 0);
       assert (nn_bind (sock, url) >= 0);

       char subType[100];
getsockopt(sock, NN_SOL_SOCKET, NN_PROTOCOL, &subType, sizeof(subType));

       if (strcmp(subType, "sub1") == 0)
       {
           while (1)
           {
               char *d = date();
               int sz_d = strlen(d) + 1; // '\0' too
               printf ("SERVER: PUBLISHING DATE %s\n", d);
               int bytes = nn_send (sock, d, sz_d, 0);
               assert (bytes == sz_d);
               sleep(1);
            }
       }
       else if (strcmp(subType, "sub2") == 0)
       {
           while (1)
           {
               const char *test = "test123";
               int sz_test = strlen(test) + 1; // '\0' too
               int bytes = nn_send (sock, test, sz_test, 0);
               assert (bytes == sz_test);
               sleep(1);
            }
       }

       return nn_shutdown (sock, 0);
 }

 int client (const char *url, const char *name)
 {
       int sock = nn_socket (AF_SP, NN_SUB);
       assert (sock >= 0);

       const char* subType = "sub1";
assert (nn_setsockopt (sock, NN_SUB, NN_SUB_SUBSCRIBE, subType, strlen(subType) + 1) >= 0);
       assert (nn_connect (sock, url) >= 0);
       while (1)
         {
               char *buf = NULL;
               int bytes = nn_recv (sock, &buf, NN_MSG, 0);
               assert (bytes >= 0);
               printf ("CLIENT (%s): RECEIVED %s\n", name, buf);
               nn_freemsg (buf);
         }
       return nn_shutdown (sock, 0);
 }

 int main (const int argc, const char **argv)
 {
       if (strncmp (SERVER, argv[1], strlen (SERVER)) == 0 && argc >= 2)
         return server (argv[2]);
else if (strncmp (CLIENT, argv[1], strlen (CLIENT)) == 0 && argc >= 3)
         return client (argv[2], argv[3]);
       else
         {
               fprintf (stderr, "Usage: pubsub %s|%s <URL> <ARG> ...\n",
                                SERVER, CLIENT);
               return 1;
         }
 }


Other related posts: