Search code examples
pubnub

PubNub: Top level wildcard can be added only if no other event handlers exist for given key and event


I Have two types of dynamic channel ids:

  1. private.x (where x is an Integer)
  2. public.x (where x is an Integer)

I'm trying to create two PubNub After-Publish-Or-Fire Functions:

  1. Bot: use for getting Bot reply for the private channels using wild card: private.* (function call to an api on my server and get a Bot reply to publish).
  2. Log: use for log all messages using wild card: * (function call to an api on my server with a message and save it to my db).

The problem is that it seems that PubNub cannot support two functions on a same channel. and event types

My questions:

  1. I don't want the log function to log only the public.* channels and that the Bot function will do both Bot and Log actions on the private.* channels. Is There other way to bypass this limitation?
  2. Do both Before-Publish-Or-Fire and After-Publish-Or-Fire events async? (maybe I can use the Before-Publish-Or-Fire for the Log function and After-Publish-Or-Fire for the Bot)?

Solution

  • PubNub Wildcards can be added only if no other event handlers exist for given key and event

    You want to reuse code in two event handlers. You provided your own answer. Your answer is an acceptable approach in your original question. And we have provided an additional way you may consider for solving this.

    • Log action on the public.* channels.
    • Bot and Log actions on the private.* channels.

    You can create a log channel which is invoked by a fire. Inside the public.* and private.* functions you will trigger a fire invoking the log channel event handler. This way you have shared code reuse for your logging mechanism.

    Multiple Channels for Logging Data to your Server

    Async vs Sync Events

    Question: Are both Before-Publish-Or-Fire and After-Publish-Or-Fire events async?

    Answer: No. Only on-after events are async.

    A natural way to think about this is that anything that needs to happen before must be blocking and non-async. Otherwise it couldn't happen before, and may end up happening after.

    Before and After Events

    Question: Can you use the Before-Publish-Or-Fire for the BOT function and After-Publish-Or-Fire for Logging)?

    Answer: Yes! This is a great way to do it.

    Before and After Event on the same PubNub Channel