Nand Arora
Nand Arora

Reputation: 111

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

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.

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.

Upvotes: 1

Views: 1253

Answers (0)

Related Questions