Publish event to specific device

I can see how I could use subscribe to catch all events, then parse some of the data in a handler to determine if it’s addressed to this specific device processing the event. But is there not a better way?

I know you can use subscribe to catch your responses, but I need to send this to a select few devices, not just the one that made the publish.

Thoughts?

When you want to publish to a specific device, the recommended method is to prefix the event name with the Device ID you are targeting. This is common for webhook responses. For example:

Particle.subscribe(System.deviceID() + "/hook-response/" + String(eventName), hookResponseHandler);
1 Like

Yes that’s how to get a response, but what if I want to send an event from cli like:

particle publish lcdHandler 00700010 --product 121212

How can I address a specific device like this?

You’d put the device ID at the beginning of the event name. For example, for device 0123456789abcdef01234567:

particle publish 0123456789abcdef01234567/lcdHandler 00700010 --product 121212

You’d subscribe on device to:

Particle.subscribe(System.deviceID() + String("/lcdHandler"), lcdHandler);

Of course if you are really targeting a single device always, you could also use a function instead of subscribing.

And if you wanted to group devices together, you could use a custom group prefix instead of the Device ID.

Thank you, that should work perfectly.

How to you address the group prefix using the cli syntax? That actually might solve a few problems I am having.

Say you had ‘GroupA’ and ‘GroupB’:

particle publish GroupA/lcdHandler 00700010 --product 121212

You’d subscribe on device to:

Particle.subscribe("GroupA/lcdHandler", lcdHandler);

and the same for GroupB.

1 Like

You just made my day. I am trying to manage 500 devices over a county, in 5 different physical locations. This really makes a massive difference. Thank you again!

1 Like

@rickkas7

particle publish GroupA/lcdHandler 00700010 --product 121212

This is not working.

Tests I've run.

particle publish GroupA/lcdHandler 00700010
Nothing happens.

particle publish lcdHandler 00700010
This triggers all devices I am owner of.

particle publish lcdHandler 00700010 --product 121212
This triggers all devices regardless of productid.

What is going on!? I really need groups to work. Why are my products, not separated streams?

I understand what I did wrong with the groups now. I forgot to subscribe to the devices.

but I still need to know why my product even streams are not separated.

particle publish GroupA/lcdHandler 00700010

Do you subscribe to the event GroupA/lcdHandler on device? That's a requirement to use that technique.

particle publish lcdHandler 00700010
This triggers all devices I am owner of.

Expected behavior.

particle publish lcdHandler 00700010 --product 121212
This triggers all devices regardless of productid.

The catch here is that if you publish the event from an access token that is also the owner of the device, then the event will go to all devices in all products owned by that user, OR the product specified. That is almost certainly not the behavior you want, but that's how it works.

The solution is to use an API user access token for the product you are targeting when you publish the event in this scenario. Because API users can't claim a device, and are specific to a product, it guarantees that the event will only go to that product.

Okay, this helps.

I hate that this is so confusing, it really doesn't help me separate different locations.

I don't want separate firmware per group.

I need to think on this.

I think I'm going to build all of them under different product ids and link them all back together on my backend. Instead of doing groups.

That seems like a reasonable choice. While grouping objects within the Particle events system is feasible, it's not what it was designed for, so if you have a different, potentially simpler way to do it, that would be my first choice.

I've run into a subscription limit problem in the past. I want to avoid it again.

So groups are a cool idea, but it just doesn't make sense in this application.

Thank you again, as always you have the answers I need.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.