Publish events directly

You can publish a CloudEvents event directly to an Eventarc Advanced bus in a supported format using the Google Cloud CLI or by sending a request to the Eventarc Publishing REST API. You can also use the Eventarc client libraries to access the Eventarc APIs from a supported language.

Before you begin

If you haven't already, enable the Eventarc API and the Eventarc Publishing API .

gcloud  
services  
 enable 
  
eventarc.googleapis.com  
eventarcpublishing.googleapis.com

Publish directly

The event message you publish directly must conform to the CloudEvents specification .

gcloud

  1. Open a terminal.

  2. You can publish events to a bus by using the gcloud eventarc message-buses publish command. For example:

    gcloud  
    eventarc  
    message-buses  
    publish  
     BUS_NAME 
      
     \ 
      
    --avro-message = 
     AVRO_MESSAGE 
    
    Or:
    gcloud  
    eventarc  
    message-buses  
    publish  
     BUS_NAME 
      
     \ 
      
    --json-message = 
     JSON_MESSAGE 
    
    Or:
    gcloud  
    eventarc  
    message-buses  
    publish  
     BUS_NAME 
      
     \ 
      
    --event-data = 
     DATA_PAYLOAD 
      
     \ 
      
    --event-id = 
     EVENT_ID 
      
     \ 
      
    --event-source = 
     EVENT_SOURCE 
      
     \ 
      
    --event-type = 
     EVENT_TYPE 
      
     \ 
      
    --event-attributes = 
     EVENT_ATTRIBUTE 
    

    Replace the following:

    • BUS_NAME : the ID or fully qualified identifier of the bus to which to publish the event.

    You must use only one of the following:

    • AVRO_MESSAGE : the event message in an Avro format according to this specification .
    • JSON_MESSAGE : the event message in a JSON format according to this specification .
    • DATA_PAYLOAD : the data of a published event.

    If using the --event-data flag, you must also use the following:

    • EVENT_ID : the event identifier. Event producers must ensure that source + id is unique for each distinct event.
    • EVENT_SOURCE : the event source of a published event.
    • EVENT_TYPE : the type of event related to the originating occurrence.

    If using the --event-data flag, you can optionally use the following:

    • EVENT_ATTRIBUTE : the attributes of a published event. You can repeat the --event-attributes flag to add more attributes.

      Note that events might include any number of additional custom CloudEvents attributes with distinct names (also known as extension attributes ).

Examples:

gcloud  
eventarc  
message-buses  
publish  
my-bus  
 \ 
  
--event-data = 
 '{"key": "hello-world-data"}' 
  
 \ 
  
--event-id = 
hello-world-id-1234  
 \ 
  
--event-source = 
hello-world-source  
 \ 
  
--event-type = 
hello-world-type  
 \ 
  
--event-attributes = 
 "datacontenttype=application/json" 
gcloud  
eventarc  
message-buses  
publish  
my-bus  
--json-message = 
 '{ 
 "specversion" : "1.0", 
 "type" :"com.example.someevent", 
 "source" : "google.cloud.storage.object.v1.finalized", 
 "id" : "A234-1234-1234", 
 "time" : "2024-04-05T17:31:00Z", 
 "bucket" : "bucketName", 
 "datacontenttype" : "application/json", 
 "data":{"key": "value"} 
 }' 

REST API

To publish an event to a bus, use the projects.locations.messageBuses.publish method.

Before using any of the request data, make the following replacements:

  • The full resource name of the bus in the format projects/ PROJECT_ID /locations/ LOCATION /messageBuses/ BUS_NAME

    Replace the following:

    • PROJECT_ID : the Google Cloud project ID for the bus project.
    • LOCATION : the region in which the bus is deployed—for example, us-central1 .
    • BUS_NAME : the name of the bus to which to publish the event.
  • SPEC_VERSION : the version of the CloudEvents specification which the event uses—for example, 1.0 .
  • EVENT_TYPE : the type of event related to the originating occurrence.
  • EVENT_SOURCE : the event source of a published event.
  • EVENT_ID : the event identifier. Producers must ensure that source + id is unique for each distinct event.
  • CONTENT_TYPE (optional): the content type of data value. If a JSON format event has no datacontenttype attribute, then it is assumed that the data is a JSON value conforming to the application/json media type.
  • DATA_PAYLOAD (optional): the event payload encoded into the media format specified by datacontenttype and adhering to dataschema when those attributes are present.

Request JSON body:

{
"jsonMessage":
  "{\"specversion\":\" SPEC_VERSION 
\",
  \"type\":\" EVENT_TYPE 
\",
  \"source\":\" EVENT_SOURCE 
\",
  \"id\":\" EVENT_ID 
\",
  \"datacontenttype\":\" CONTENT_TYPE 
\",
  \"data\":\" DATA_PAYLOAD 
\"}"
}

To send your request, expand one of these options:

If successful, the server returns an HTTP 200 OK status code and the empty response body in JSON format:

200 OK

{}

What's next

Create a Mobile Website
View Site in Mobile | Classic
Share by: