Newer Version Available

This content describes an older version of this product. View Latest

Subscribe to a Custom Platform Event

Use EMP Connector to subscribe to the Low_Ink__e custom platform event that you defined earlier.
  1. Run the LoginExample class and provide arguments.
    1. In Package Explorer, navigate to the LoginExample.java file. Right-click the file, and select Run As | Run Configurations.
    2. On the Arguments tab, add values for the following arguments, separated by a space.
      Argument Value
      username Your Salesforce username
      password Your Salesforce password. Append a security token to your password if you haven't set up a range of trusted IP addresses. For more information, see Reset Your Security Token and Set Trusted IP Ranges for Your Organization in Salesforce Help.
      channel The channel name for the event: /event/Low_Ink__e.
    3. Click Run.

    The sample is now subscribed to the event channel and is listening to event notifications. As soon as an event notification is generated and received, the tool prints it to the console.

    Optionally, to receive different events, you can include a replay ID as the last argument. Valid values are:
    • –1: Get all new events sent after subscription. This option is the default.
    • –2: Get all new events sent after subscription and all past events within the retention window. Use -2 sparingly. If a large volume of event messages is stored, retrieving all event messages can slow performance.
    • Specific value: Get all events that occurred after the event with the specified replay ID.
  2. To generate an event message for the custom platform event, publish an event message by running Apex in the Developer Console.
    1. In Salesforce Classic, select your name | Developer Console.
    2. In Lightning Experience, click the quick access menu (Setup gear icon), and select Developer Console.
    3. In the Developer Console, select Debug | Open Execute Anonymous Window.
    4. In the new window, replace any code with this Apex snippet, which publishes the platform event.
      1// Create event instance.
      2Low_Ink__e event = new Low_Ink__e(Printer_Model__c='XZO-5', Serial_Number__c='12345', 
      3              Ink_Percentage__c=0.2);
      4
      5// Publish event.
      6Database.SaveResult sr = EventBus.publish(event);
      7
      8// Inspect publishing result for each event
      9if (sr.isSuccess()) {
      10    System.debug('Successfully published event.');
      11} else {
      12    for(Database.Error err : sr.getErrors()) {
      13        System.debug('Error returned: ' +
      14                    err.getStatusCode() +
      15                    ' - ' +
      16                    err.getMessage());
      17    }
      18}
    5. Click Execute. After the platform event is published, EMP Connector receives an event notification, which is printed in the console. The output looks similar to the following.
      1Subscribed: Subscription [/event/Low_Ink__e:-1]
      2Received:
      3{  
      4   "schema":"3l1laWb62nM8omMU0waLdg",
      5   "payload":{  
      6      "Serial_Number__c":"12345",
      7      "CreatedById":"00550000001N45jAAC",
      8      "CreatedDate":"2018-08-15T21:49:44Z",
      9      "Ink_Percentage__c":0.2,
      10      "Printer_Model__c":"XZO-5"
      11   },
      12   "event":{  
      13      "replayId":1
      14   }
      15}

Generally, don’t handle usernames and passwords of others when running code in production. In a production environment, delegate the login to OAuth. The BearerTokenExample.java class uses OAuth authentication.

Note