Skip to content

Publishing to topic in .SubscriptionAdded seems to be impossible? #340

@aaaaaaaaargh

Description

@aaaaaaaaargh

Hey, I'll try to keep it short, so bear with me. I'm having the following scenario: I want to publish something to a subject as soon as the remote side has subscribed to the topic. This code demonstrates what I'm trying to achieve:

// this is on router side
var topic = realm.TopicContainer.CreateTopicByUri(topicUri, false);
topic.SubscriptionAdded += (sender, args) => 
{
    realm.TopicContainer.Publish(WampObjectFormatter.Value, new PublishOptions(), topicUrl), new object[]{...});
}

However, after investigating why the object is not being received on subscriber side it turns out it is impossible to do this in the event listener due to the following implementation in WampRawTopic:

public void Subscribe(ISubscribeRequest<TMessage> request, SubscribeOptions options)
    {
      RemoteWampTopicSubscriber subscriber = new RemoteWampTopicSubscriber(this.SubscriptionId, (IWampSubscriber) request.Client);
      WampRawTopic<TMessage>.RemoteObserver remoteObserver = this.mSubscriberBook.Subscribe(request.Client);
      if (!remoteObserver.IsOpen)
        this.RaiseSubscriptionAdding(subscriber, options);
      request.Subscribed(this.SubscriptionId);
      if (remoteObserver.IsOpen)
        return;
      this.RaiseSubscriptionAdded(subscriber, options); // <--- this is where the code above is executed
      remoteObserver.Open(); <-- this is where the observer is opened
    }

So, accoring to this method (also WampRawTopic) any published message is discarded unless the remote observer has been opened:

public void Message(WampMessage<object> message)
{
  if (!this.IsOpen)
    return; // <-- this is what's happening in my case
  this.mClient.Message(message);
}

In conclusion seems to be impossible to publish messages when the subscription has been opened. Is there any recommended way of doing this? I'm thinking about delaying the execution with a deferred Task but that feels really dirty and not very good in terms of perfomance. Do you have any suggestions how this could be solved?

Thank in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions