10 webhooks but only 4 subscriptions?

Hi,

I was working on a project that utilized 10 different webhooks at different times in the sketch. The photon documentation says you can register up to 20 webhooks with a single device, but the particle.subscribe( ) function says :

“NOTE: A device can register up to 4 event handlers. This means you can call Particle.subscribe() a maximum of 4 times; after that it will return false.”

Am I wrong to think I cannot get data from 10 separate webhooks in one sketch?

I can see the webhook data for all 10 in the console but when I try to print the data on an LCD screen I can’t seem to print more than 4 in one sketch. They all work and it doesn’t matter which 4 I choose as long as I don’t subscribe to more than 4.

Is that just the way it is? Is there a workaround or do I need to look at other ways or consolidating the webhook data before the photon makes the call.

Thanks for any advice.

Hi @extrasleepy

The event name can be used a prefix so if you have a handler for event “foo” it will match “foo”, “foobar”, “foobaz”, “foopperflopper”, etc.

3 Likes

As @bko said, prefix filtering is the way to use these subscriptions by design, not as a workaround.

Particle.subscribe("hook-response/", ...) will setup one subscription for all webhook responses and you can then distinguish which response it was in your code by looking at the even name (first parameter of the handler)

1 Like

OK. I think there’s something I don’t understand here. I tried below. Which seemed to work until I started getting ESOCKETTIMEOUT in the console. Still something wrong?

void setup() {
    Particle.subscribe("hook-response/", myHandler, MY_DEVICES);
}

void loop() {
    String data = String("getting data");
    Particle.publish("one", data, PRIVATE);
    delay(60000);
    Particle.publish("two", data, PRIVATE);
    delay(60000);
    Particle.publish("three", data, PRIVATE);
    delay(60000);
    Particle.publish("four", data, PRIVATE);
    delay(60000);
    Particle.publish("five", data, PRIVATE);
    delay(60000);
    Particle.publish("six", data, PRIVATE);
    delay(60000);
    Particle.publish("seven", data, PRIVATE);
    delay(60000);
    Particle.publish("eight", data, PRIVATE);
    delay(60000);
    Particle.publish("nine", data, PRIVATE);
    delay(60000);
    Particle.publish("ten", data, PRIVATE);
    delay(60000);
}

void myHandler(const char *event, const char *data) {

    String text = String(data);
    
}

Should I make my event names more similar? Like the foo, foobar food suggestion?

Are you expecting to catch your own events or the respective webhook responses?

The hook-response/ filter will only catch the latter ones.
If you want to test the former, you need to prefix your event names with ... well ... a fix term like

  Particle.subscribe("number_", myHandler, MY_DEVICES);
  Particle.publish("number_one", data, PRIVATE);

BTW, I'd rather avoid String and go with

  char data[32] = "getting data";

And all these delays will also impact responsiveness of the cloud task as in these cases the cloud keeping only happens once per second - instead of aprox. 1000 times with non-blocking programming techniques.

About the ESOCKETTIMEOUT have a read here

Thank you. Let me try some variations based on your advice. I'm getting data from an API and I want different data from a different webhook each minute (thus the delays).

Hi ScruffR and bko,

Just letting you know, thanks to your advice, I got my 10 webhooks working the way I want with just 3 subscriptions. Thanks for explaining the prefix filtering. You are awesome.

3 Likes