Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to publish data between Node.js modules? #165

Closed
bennycode opened this issue Dec 5, 2016 · 2 comments
Closed

How to publish data between Node.js modules? #165

bennycode opened this issue Dec 5, 2016 · 2 comments

Comments

@bennycode
Copy link

bennycode commented Dec 5, 2016

Hello, I have two node modules (A & B).

In Node module A I have a Class A which defines the following:

module_a\ClassA.js

ClassA.prototype.postEvent = function() {
  postal.subscribe({
    channel: "my-channel",
    topic: "my-topic",
    callback: function(data, envelope) {
      console.log('Received data in Module A.', data, envelope);
    }
  });

  postal.publish({
    channel: "my-channel",
    topic: "my-topic",
    data: {
      text: 'Hello World!'
    }
  });
}

As you can see, Class A subscribes to a topic and then publishes data for this specific topic.

Now I want to subscribe to the same topic in module B (which initiates Class A from module A):

module_b\app.js

var ModuleA = require('module_a');
var postal = require('postal');

postal.subscribe({
  channel: "my-channel",
  topic: "my-topic",
  callback: function(data, envelope) {
    console.log('Received data in Module B.', data, envelope);
  }
});

var classA = new ModuleA.ClassA();
classA.postEvent();

Unfortunately, the subscription of Module B doesn't get invoked when calling classA.postEvent();. Only Class A's subscription gets invoked.

Can you tell me what I am doing wrong? Do I have to take care of something additional when publishing events for other Node.js modules?

@ifandelse
Copy link
Contributor

@bennyn - quick question, are both modules pulling in the same version of postal? (i.e. - are these separate npm modules that have different version ranges of postal in their package.json?)

@bennycode
Copy link
Author

@ifandelse That was exactly the issue!

I declared postal in the package.json files of both modules. But because ModuleB is using ModuleA, it receives postal already as transitive dependency from ModuleA, so I don't need to declare postal as dependency again in the package.json file of ModuleB (in fact this causes problems, having both modules using different postal instances).

Thank you for your help! Now I am eagerly waiting for postal v2.0.5. 😃

Thumbs up and keep the good work!

Will close this issue here as solved. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants