Giter VIP home page Giter VIP logo

Comments (5)

kousu avatar kousu commented on May 22, 2024

This is my python bias speaking, but There Should On Be One Way To Do It. The first form is more intuitive, and keeps the code shorter (I don't understand why I care about a subscription; that should be an internal object).

What I want is the behaviour of jQuery: .subscribe() should append handlers to a list, and you should have an .unsubscribe() which optionally takes the (previously subscribed) function to unsubscribe.

My use case is this: I have a stream of numbers coming from my server, and I want to feed them to d3 plots, to a simulated robot on a plot, and to the console for debugging. Maybe the debug prints can sit in one or the other handler, but it would be awkward (and cut across js files) to have both viewing features in one function. If Autobahn doesn't do multiplexing, I'll need the function I pass to .subscribe() be a multiplexer instead of directly being handlers I actually care about.

from autobahn-js.

oberstet avatar oberstet commented on May 22, 2024

Subscriptions are objects since for unsubscribing at the WAMP message level, you need to know the subscription ID - which is stored inside the JS subscription object returned.

Not sure if I get you: do you want attaching multiple handlers like this?

session.subscribe('com.myapp.topic1', handler1);
session.subscribe('com.myapp.topic1', handler2);

If so: this was the API of WAMP v1. Internally, subscriptions would be a dictionary indexed by topic. With WAMP v2 and pattern based subscriptions, this naive view of "topic ~ subscription" no longer holds true. Now, the subscription ID is returned asynchronously from the broker upon subscribing.

from autobahn-js.

oberstet avatar oberstet commented on May 22, 2024

Removing a handler from the list would work as "unwatching":

session.subscribe('com.myapp.topic1').then(
   function (subscription) {
      subscription.watch(handler1);

      // stuff

      subscription.unwatch(handler1);
   }
);

Calling session.subscribe multiple times returns the same subscription object.

For this we need to either use a broker to detect a duplicate subscription and return the existing subscription ID. Or, have a client-side map that uses key material from topic and Options.

from autobahn-js.

goeddea avatar goeddea commented on May 22, 2024

Agree with kousu that I shouldn't have to care where in my code I subscribe to a topic, and how often this happens.

My suggestion:

  • Allow multiple, separate subscriptions using the simple syntax of
    session.subscribe('com.myapp.topic1', handler1);
  • These all return separate subscription objects / IDs, and can be unsubscribed individually.
  • The library creates these objects, but only uses a single subscription under the hood

from autobahn-js.

codegastudio avatar codegastudio commented on May 22, 2024

Hi,

I had many subscription done on same topic name in different angular 2 component.

// in component 1
session.subscribe('com.myapp.topic1', handlerComponent1)

// in component 2
session.subscribe('com.myapp.topic1', handlerComponent2)

I need to unsubscribe them.

var subscriptions = session.subscriptions;
for (var index in subscriptions) {
  let subs = subscriptions[index];
  for (let sub_index in subs) {
    let sub = subscriptions[index][sub_index];
    let topic = sub.topic;
    sub.unsubscribe().then(res => {
      console.log(topic, 'unsubscribe', res);
    });
  }
}

And this now than troubles come.
I had all the time one subscription witch is not unsubscribe.

Any idea ?

UPDATE
I found a way !
Subscriptions as grouped in different arrays.
So i concat all of them and loop on each subscription in this single new array and this work.
Some unsubscribe Subscription promise return false but they are not anymore activated so i don't catch anymore handler on unsubscribed topic.

var subscriptions = [];
for (let index in session.subscriptions)
  subscriptions = subscriptions.concat(session.subscriptions[index]);
  for (let index in subscriptions) {
    let subscription = subscriptions[index];
    let topic = subscription.topic;
    session.unsubscribe(subscription).then(res => {
       console.log("unsubscribe", topic, res);
    });
 }

from autobahn-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.