Issue with multiple instances of Particle.subscribe() using same handler above OS 1.2.1

I’ve got on Photon publishing various events and another set up subscribed to them.

The subscribing photon was unable to handle more than one subscription with Device OS 1.4.4. When I changed it to Device OS 1.2.1, it works.

Below are the two simple subscribe statements. With 1.4.4 whichever one came first in the file worked, and the second one never triggered. Swap the order and the one that is now first works, and the second doesn’t.

Particle.subscribe("LedOn", onHandler, MY_DEVICES);
Particle.subscribe("LedOff", onHandler, MY_DEVICES);

Same exact code working fine for me on 1.2.1 and both trigger as expected. I’m just tinkering around at home, so this doesn’t post any real issues for me. But flagging in case others have the same issue or there is something that I’m missing and need to learn that may cause problems for me down the road!

There is this post that is close, but a different issue: Multiple Particle.subscribes strange behavior


I will guess here that in OS 1.4.4 there is an assertion to stop having different event subscriptions going to the same handler. I can’t point you at the enhancement ID or bug fix.

Generally, I would rather have the minimum number of subscribes and then handle the difference in the handler by checking the data.

Particle.subscribe("Led", onOffHandler, MY_DEVICES);

void onOffHandler(const char *event, const char *command)
    if      (strncmp(command, "on", 2) == 0)               //Led On
        //turn LED on
    else if (strncmp(command, "off", 3)                     //Led Off
         //turn LED Off

Thanks @armor - that makes sense and for my use case those two could definitely be combined!

Though I did actually try it with a second handler to see if that was the issue - and still had the same failure of the second one. So at least in my case, it wasn’t an issue due to a shared handler.

Odd - perhaps an undocumented change and I’ve got something else wrong…wouldn’t put it past me!

When you see issues with one version it may be worth trying any more recent version whether that assumed “regression” already got caught and fixed or not.
Currently we are seeing v1.5.0-rc.2 so maybe give this a try.

If the issue persists, you can file an issue on the device OS GitHub repo.

BTW, when and where do you register the subscription?
Timing might be an issue.

So you did this ... and it didn't work?

Particle.subscribe("Ledon", onHandler, MY_DEVICES);
Particle.subscribe("Ledoff", offHandler, MY_DEVICES);

If this doesn't work with your code on 1.4.4 then that is an error - 5 subscriptions are allowed.

Thanks both! @ScruffR thanks for the tip. I’ve tried it and it is still an issue in 1.5.0 rc1 (2 wouldn’t build for me for some reason, failed to magenta)

@armor - I was certain (whoops!) I tried different handlers and it didn’t work, but to be safe I went back and tested again. You are correct - in 1.4.x - separate handlers is fine.

I tried all 1.4.x and 1.3.1 and same issue. It isn’t back until 1.2.1 that the same handler seems to allow multiple subscriptions. May well be an intentional change given it has been around for a bit?

EDIT: have updated the post title to clarify

1 Like