How to subscribe to Salesforce Platform Event in a Python application?

998 views Asked by At

I have been working on an app, in Python, to subscribe to the Platform Events generated in Salesforce. I know the basic working on Platform Events, it uses the Bayeux Protocol, long polling, and requires a cometD client to subscribe to the event, but I'm not able to translate this to code.

I found a library for Bayeux Protocol through which I am able to authenticate my client with Salesforce, however, I'm not sure how will I receive the events.

  • Firstly, my app is not on any network, it is running on my local machine, I'm not sure how it will access the "bus" on which Platform Events are published.
  • Secondly, there is a function to subscribe to the events in that library but as soon as my app executes all the lines of code it has, it terminates, it's not waiting for an event to happen on Salesforce which will generate the Platform Event so my app could read it.

If someone has implemented it in Python, please share some code/library that I can refer to implement this or help me understand how to use this library to listen to the Platform Events and do I need to put my application on a web server?

I am sharing the code I've written so far to implement this and it's not throwing any error.

from python_bayeux import BayeuxClient as Client
import requests

def cb(data):
  print('callback')
  print(data)

s = requests.Session()
s.headers.update({'Authorization': 'OAuth <ACCESS_TOKEN>'})

client = Client("https://instance.my.salesforce.com/cometd/48.0/", oauth_session=s, start=False)
client.handshake()
client.connect()
client.subscribe("event/File_Upload__e", callback = cb)

File_Upload__e - This is the Platform Event I've created.

0

There are 0 answers