Platform Events – Salesforce Integrations on Easy Mode

I spent some time presenting about Platform Events last week at Tahoe Dreaming and I felt like I needed to put something out there about how freaking cool Platform Events are. I have spent the last few weeks preparing for that presentation and it would be a shame to not share with everyone the ease and greatness that comes with Platform Events (PE).

First, I recommend you do the Trailheads for PE. It’ll give you a good idea of what they are, what they aren’t and some of the funky gotchas that you would normally think you would be able to do with things inside of Salesforce but can’t.

Let’s review some of the big ones that stand out for me:

  1. PE are READ ONLY
  2. PE cannot be queried directly from Salesforce
  3. PE are only stored for 24 hours
  4. You will have a limit to the number of PE you can publish hourly
  5. Salesforce recommends less than 5M daily messages over CometD

Other than that, get crackin!

Here is my presentation from Tahoe Dreaming if you want the slide deck for some reason.

Anyway, let’s review some of the goodness that comes with PE:

  1. Uses the Publisher/Subscriber mode (pub/sub)
  2. Updates are seen instantly across the pipe to subscribers
  3. Can connect Triggers, Processes and Flow to PE to process business logic
  4. Can create PE with Triggers, Processes and Flow
  5. Ability to use Replay if something doesn’t come across the pipe (see Replaying Past Events)

Let’s see it in action, shall we? Before continuing further, please make sure you understand the purpose of Platform Events and when/why you would want to use them. NOTE: PE are NOT for doing automation inside of Salesforce alone. You would just create your own triggers, Processes and Flows.

I have a simple node.js application that uses nForce for basic Salesforce connectivity and functionality. In the app setup, I determine which PE I want to subscribe to. On startup, we will see the app being subscribed:

PE_startup

In this case, we are subscribed to a PE called ‘Custom_Integration__e’. What makes nForce so powerful is that once you authenticate, you can subscribe to a PE with a single line once the client is established:

var client = sfdc.createStreamClient();
var con = client.subscribe({ topic: '/event/Custom_Integration__e', isPlatformEvent: true, oauth: oauthtoken });

The last line of the previous GIF tells is that the client is subscribed to the topic successfully and can now accept PE from Custom_Integration__e.

Now, let’s do a test and create a PE inside of Salesforce and see how quickly the subscriber sees the update:

PE_publish

On the left side, we have the Developer Console creating a PE with:

List<Custom_Integration__e> notes = new List<Custom_Integration__e>();
Custom_Integration__e note = new Custom_Integration__e(Message__c = 'test from Salesforce');
notes.add(note);

List<Database.SaveResult> results = EventBus.publish(notes);

On the right side, we see the app being notified of the new record almost instantly. This data is being saved to Saleforce and immediately notifying the node app within a second. That is freakin amazing.

There is another use case, where many apps can be subscribed to PE and either publishing or being notified of these updates. We can simulate this with Postman and posting a Custom_Integration__e PE and see how quickly we are notified in our app.

So, let’s set up Postman to be able to do this. First, we need to download Postman if you already have not. Second, we need to set up our org to be used with Postman using a Connected App. Once we do that, we can GET or POST in Postman really easily with a few steps:

  1. Request a new Oauth token from Salesforce using the step-by-step above
  2. Using that token, attempt to POST to the PE endpoint (base instance URL + /services/data/v41.0/sobjects/***object name***/)
  3. In the body of the POST, include the data we want:
{
"Message__c" : "test platform events"
}

So far, we have just been using this custom field on our PE, Message__c, so let’s stick with that. Now, let’s POST this sucker and see what happens:

PE_postman

That is freaking amazing. Honestly. Almost immediately after I post the PE, it is available in my local app.

We can also add attributes to our PE on the fly and they show up in our subscription without any extra work. Let’s add a new field called ‘Test’. We just navigate to our Platform Event in Salesforce and create a new field just like we would on a custom object. We can use Postman again to add data to this field for our subscribers:

{
"Message__c" : "test platform events",
"Test__c" : "This is a test"
}

PE_postman1

Without doing anything else besides adding the field in Salesforce, it becomes and attribute of the subscription immediately. I think this is such a cool feature.

This is just a quick run through of the basic functionality. The missing pieces here are obviously the automation that would be necessary for this to be a functional app, however that was not the purpose. I think the applications of PE are going to be really cool and will replace how we currently build things in Salesforce, but also potentially allow you to retire messaging services or servers like Biztalk or Oracle.

I would be doing you a disservice if I didn’t mention this: while building the PE in Salesforce makes Integrations really easy on the Salesforce side, the Integrations built on the other side could be more complex because of the new pattern you will be implementing of pub/sub. I think this is still worth the effort as after you have figured out how to do it on the first Integration, all future PE Integrations will follow the same pattern and hopefully can be re-used.

Here are some resources that GREALY helped me:

  1. Doug Ayers PE project
  2. Jay Hursts PE project using node
  3. Salesforce Developer Blog: Using Platform Events to Integrate Salesforce and Node.js

5 thoughts on “Platform Events – Salesforce Integrations on Easy Mode

  1. Hello Mark, Thanks for the amazing explanation and work. One quick concern, can we subscribe PE in postman?

    Cheers..!!!

    Like

  2. why dont you post the complete js code / app.js instead of just var client = … and var con = …? Your post is not very comprehensible unfortunately.

    Like

    1. That’s fair. My hope was that you would be able to see how easy it could be if you already had something set up.

      Look at Jay Hursts app which will get you up and running really quick with node and nforce which makes this whole thing really simple: https://github.com/jthurst01/platform-events-socketio

      I already had a node app that did a lot of other stuff but the use of nForce and the ease of subscribing makes it a no-brainer. That should hopefully make more sense.

      Ideally though, you should be able to use whatever framework or application and connect to platform events using cometD anyway.

      Like

Leave a comment