Step 4: Publish and Subscribe to Platform Events

Use the Java examples for the Subscribe RPC and PublishStream RPC to try out publishing and receiving events.


  • If you’re using a Developer Edition org and you set up a namespace prefix, modify the Java code that builds the event message to prepend the namespace prefix to the platform event fields. In java/src/main/java/utility/, modify the createEventMessage and createEventMessages methods to include the namespace prefix in the field names.
  1. In a Terminal window, navigate to the top-level java folder.

  2. To run the Subscribe RPC example, enter: ./ genericpubsub.Subscribe

  3. The Subscribe example prints out logging info on the terminal, similar to:

    Because no events were published, the count of received events is zero.

  4. Before you can publish events, modify the code to specify the event fields and values to publish. In this quick start, you modify the code to supply the user ID for the CreatedById field in the event message. You use the sample values for the other event fields. In a text editor or an IDE, such as Visual Studio Code, open the java/src/main/java/utility/ from your pub-sub-api folder.

  5. There are several createEventMessage methods and one createEventMessages method that build the event messages. This method is used in

    These methods are used in In this quick start, we use with the default argument value of SINGLE_PUBLISH_REQUEST of true, so the createEventMessages method is used.

    Provide a valid CreatedById value for the published event message in this method. Replace the <User_Id> placeholder value of the CreatedById field with a valid user ID in your Salesforce org. To get the user ID, see Find the Salesforce ID for a User or Profile.

  6. Optional. If you want to supply other event fields and values, make additional changes.

  7. To publish all events in one publish request using the createEventMessages method, in java/src/main/resources/arguments.yaml, set SINGLE_PUBLISH_REQUEST to true: SINGLE_PUBLISH_REQUEST: true

  8. In a new terminal window, navigate to the top-level java folder in your pub-sub-api folder.

  9. To build the Java client, enter: mvn clean install

  10. To run the PublishStream RPC example, enter: ./ genericpubsub.PublishStream The example publishes five events as set in the arguments.yaml file’s default configuration. It prints out logging information to the terminal.

  11. Return to the terminal for the Subscribe example. The output shows the events received.

To learn more about the example code, see Implementation and Limitations in the Readme page of the pub-sub-api GitHub repository.

The client code for the Pub/Sub API RPC methods is in the genericpubsub folder. To check out the implementation for each RPC method, open the corresponding Java file in the genericpubsub folder. For the PublishStream example, open And for the Subscribe example, open

For more information about the PublishStream and Subscribe RPC methods, see PublishStream RPC Method and Subscribe RPC Method.

To run other RPC methods, use this command format: ./ <package_name>.<example_class_name>