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 have set up a namespace prefix, modify the Java code that builds the event record to prepend the namespace prefix to the platform event fields. In java/src/main/java/utility/, modify the two createCarMaintenanceRecord() methods to include the namespace prefix in the field names.
  1. In a Terminal window, return to the top-level java folder.

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

    1. The example subscribes to the five events that were published.
  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. To run the PublishStream RPC example, open another Terminal window. Go to the java folder and then enter ./ genericpubsub.PublishStream

    1. The example publishes five events as set in the arguments.yaml file's default configuration.
  5. The PublishStream example publishes five events by default. It prints out logging information to the terminal similar to this example.

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

    The Java example publishes batches (PublishRequests) containing one event each. Because the PublishStream RPC method doesn’t guarantee the order of batches, the order of events isn’t preserved in this example. However, if a PublishRequest contains more than one event, PublishStream preserves the order of events within each batch (PublishRequest). For more information, see the PublishStream RPC Method.

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 example, 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>