Newer Version Available

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

Subscribe to a Standard Platform Event Channel

Use EMP Connector to subscribe to the channel of the LoginEventStream standard platform event. This channel tracks user logins in real time and is part of Real-time Event Monitoring.
  1. In Setup, enable streaming for the LoginEventStream event on the Event Manager page.

    LoginEventStream is part of Real-Time Event Monitoring. To enable streaming for this event in Event Manager and subscribe to the channel, you must have the Shield Event Monitoring add-on and the View Real-Time Event Monitoring Data permission enabled. For more information, see Real-Time Event Monitoring in Salesforce Help.

    Note

  2. In Eclipse, 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
      channel The channel name for the event. For the LoginEventStream standard event, provide /event/LoginEventStream.

      To perform this quick start with another standard event, pass in a channel name in the following format: /event/Event_Name

    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 number — Get all events that occurred after the event with the specified replay ID.
  3. To generate an event message for a standard platform event, perform the action that fires the event. For LoginEventStream, log in to Salesforce.
    1. In a browser window, navigate to https://login.salesforce.com.
    2. Enter your Salesforce username and password (or the credentials of another user in your org), and click Log In.
    3. After you log in, EMP Connector receives an event notification for the login action. The event message is printed in the console. The output looks similar to the following.
      1Subscribed: Subscription [/event/LoginEventStream:-1]
      2Received:
      3{  
      4   "schema":"3J6UjLfL6cDEeBI84DSyTA",
      5   "payload":{  
      6      "EventDate":"2019-01-04T21:32:15.000Z",
      7      "AuthServiceId":null,
      8      "Platform":"Mac OSX",
      9      "EvaluationTime":0.0,
      10      "CipherSuite":"ECDHE-RSA-AES256-GCM-SHA384",
      11      "ClientVersion":"N/A",
      12      "LoginGeoId":"04F2J00006PqzoY",
      13      "LoginUrl":"login.salesforce.com",
      14      "LoginHistoryId":"0Ya2J0000Dt8t5aSQA",
      15      "CreatedById":"00550000001ZtKcAAK",
      16      "SessionKey":null,
      17      "ApiType":"N/A",
      18      "LoginType":"Application",
      19      "PolicyOutcome":null,
      20      "Status":"Success",
      21      "AdditionalInfo":"{}",
      22      "ApiVersion":"N/A",
      23      "EventIdentifier":"eeccf731-2585-4a40-bfa5-770e31d6c2ab",
      24      "RelatedEventIdentifier":null,
      25      "SourceIp":"Salesforce.com IP",
      26      "Username":"joe.smith@acme.com",
      27      "UserId":"00550000001N45jAAC",
      28      "CreatedDate":"2019-01-04T21:32:19.188Z",
      29      "TlsProtocol":"TLS 1.2",
      30      "LoginKey":"QuEoTPHKy22V68XV",
      31      "Application":"Browser",
      32      "UserType":"Standard",
      33      "PolicyId":null,
      34      "SessionLevel":"STANDARD",
      35      "Browser":"Chrome 71"
      36   },
      37   "event":{  
      38      "replayId":2540
      39   }
      40}

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