0

I have two decoupled components running on the one page and would like to use Postal.js to send a message when one is clicked to the other.

When the first component is clicked it publishes a post:

postal.publish({
  channel: 'carts',
  topic: 'item.add',
  data: {
    quantity: newQuantity
  }
});

Then in the second component I subscribe to that channel in componentDidMount():

postal.subscribe({
  channel: 'carts',
  topic: 'item.add',
  callback: this.handleStorageChange.bind(this)
});


When I add a wiretap to the subscribed component it shows that the channel was successfully subscribed to but never shows the post from the other component:

{"channel":"postal","topic":"subscription.created","data":{"event":"subscription.created","channel":"carts","topic":"item.add"},"timeStamp":"2018-03-14T01:59:38.309Z"}

When I add a wiretap to the posting component it shows that the message was posted:

{"channel":"carts","topic":"item.add","data":{"quantity":34},"timeStamp":"2018-03-14T01:59:41.844Z"}

What am I doing wrong here?

Matt Hough
  • 1,049
  • 2
  • 13
  • 27

2 Answers2

1

I tried creating a minimal snippet to make communication between two components. And I was able to receive the data on subscribing component.

You can refer this snippet, https://codesandbox.io/s/kxqw6npm05

Saba
  • 3,418
  • 2
  • 21
  • 34
  • I've just checked my code with yours and it looks no different. I'm using webpack with rails to render these two components separately in my view. Could that make a difference? – Matt Hough Mar 14 '18 at 05:53
  • Apparently webpack was exactly the issue. I was rendering them in different 'packs' and they couldn't communicate with each other. – Matt Hough Mar 14 '18 at 13:14
0

It turns out having my two components in different webpack packs caused issues with communication between the two.

Packing the two components together fixed the issue.

Matt Hough
  • 1,049
  • 2
  • 13
  • 27