The community forums are in BETA and closed to the public.

Syncing the Subscription

I have multiple publish and subscriptions in the same page. How to sync one subscription finished after that the publish should be happen. Is there any ways to sync this. Let me explain my scenario in my application:

  • first i need to get all the driver location
  • after that only i am find my nearest driver.

In this i have subscribed the driver locations each driver posting their location in periodic manner. But the nearest location function called before all driver location came in? anyway to sync these two events?

You need to next the next call in the callback of the first call something like this.

function subscribeCallback(msg) {
    console.log(JSON.stringify(msg));
    pubnub.publish({channel: 'drivers', message: "hello drivers"});
}

pubnub.subscribe({
    channel: 'locations',
    message: subscribeCallback
});

Above, the subscribe will get executed. When I message is received, the subscribeCallback function is executed and in there the publish is executed. It's a little messy but there are more advanced design patterns for handling this sort of implementation that is beyond the context of this forum. But hopefully this will get you started in the right direction.


Comments to this discussion are now closed!