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.


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);

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