device-os icon indicating copy to clipboard operation
device-os copied to clipboard

Add support for event content types

Open sergeuz opened this issue 1 year ago • 1 comments

Description

This PR extends the Particle.publish() and Particle.subscribe() APIs to support events with non-plain text data. The actual format of the data is determined by its content type:

enum class ContentType {
    TEXT, // text/plain; charset=utf-8
    JPEG, // image/jpeg
    PNG, // image/png
    BINARY, // application/octet-stream
    JSON, // application/json
    CBOR // application/cbor
};

Examples

Publishing JSON data with an explicit content type:

Particle.publish("my_event", "{\"abc\":123}", ContentType::JSON);

Publishing arbitrary binary data:

char buf[] = { 0x00, 0x01, 0x02, 0x04 };
Particle.publish("my_event", buf, sizeof(buf), ContentType::BINARY);

Subscribing to binary events:

void eventHandler(const char* name, const char* data, size_t size, ContentType type) {
    Log.info("Received event: %s; content type: %d; size: %u", name, (int)type, (unsigned)size);
    Log.dump(data, size);
    Log.print("\r\n");
}

void setup() {
    Particle.subscribe("my_event", eventHandler);
    // ...
}

When publishing/subscribing to events with a content type via the HTTP API, the event data is expected to be encoded as a data URI.

References

sergeuz avatar Jul 23 '24 16:07 sergeuz