Search code examples
zeromqnetmq

ZeroMQ publisher socket - raise event on subscription


I was wondering if there is a way to raise events on new subscription to a publisher socket

For example:

        PublisherSocket publisher = new PublisherSocket();

        publisher.Bind("tcp://*:5555");

        NetMQPoller poller = new NetMQPoller { publisher };
        poller.RunAsync();

        poller.OnSubscription += topic =>
        {
            AddToPool(topic);
        };

        Task.Factory.StartNew(() =>
        {
            SubscriberSocket sub = new SubscriberSocket();
            sub.Connect("tcp://127.0.0.1:5555");

            Thread.Sleep(1000);
            sub.Subscribe("A");
        }, TaskCreationOptions.LongRunning);

        publisher.ReceiveReady += Publisher_ReceiveReady;

Of course, OnSubscription doesn't really exist, but I was wondering if there is any way around it.

I need my server to be aware of all the subscriptions.

I though about two ways to implement it:

  1. Create an additional router socket in the server, all subscriptions will be sent both to the publisher socket and to the router socket (unsubscriptions as well). This will allow me to poll the subscriptions from the router.

  2. Not use publisher/subscriber at all, create all the pubsub mechanism with router/dealer.

What would you suggest me do?


Solution

  • If you use an XPUB rather than PUB socket you can receive the subscription messages as you would regular messages on any other socket type.