Hey I’m looking the same as you to access pubsub using python. I just got this code running, maybe this can help you with the basics. Eventually we need to implement a heartbeat to maintain the connection.
channel-points-channel-v1.125627324 is my id, you need to change the value with your own id. You should check the OAuth guide (https://dev.twitch.tv/docs/authentication/getting-tokens-oauth).
Also, one thing to notice is that when you request your OAuth you need to request the specifics scopes for the topic you want to listen (https://dev.twitch.tv/docs/pubsub#topics)
import websocket
try:
import thread
except ImportError:
import _thread as thread
import time
oauth = XXXXXXXXXXX #Your Oauth Code
listenRedeems = {“type”: ‘LISTEN’,‘nonce’: ‘44h1k13746815ab1r2’,‘data’:{‘topics’: [‘channel-points-channel-v1.125627324’],‘auth_token’: ‘%s’ % (oauth))}}
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
def run(*args):
ws.send(json.dumps(listenRedeems))
thread.start_new_thread(run, ())
if __ name __ == “__ main __”:
websocket.enableTrace(True)
ws = websocket.WebSocketApp(“wss://pubsub-edge.twitch.tv”,
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()