I just ran into this same problem.
I was able to print the session id from the welcome message just like in the code from above.
I made sure the welcome message was received and added the session id in the post request.
I was also sure the post request went off before the default 10 second keepalive timeout was over.
async def hello():
uri = 'wss://eventsub.wss.twitch.tv/ws?keepalive_timeout_seconds=30'
async with websockets.connect(uri) as websocket:
name = "test"
await websocket.send(name)
print(f'client sent: {name}')
greeting = await websocket.recv()
print(f'received: {greeting}')
//received: {"metadata":{"message_id":"<id>,"message_type":"session_welcome","message_timestamp":"<timestamp>"},"payload":{"session":{"id":"<id>","status":"connected","connected_at":"<connected at>","keepalive_timeout_seconds":30,"reconnect_url":null,"recovery_url":null}}}
greeting = json.loads(greeting)
session_id = greeting['payload']['session']['id']
print(f'session_id: {session_id}')
subscription_params = {
"Content-Type": "application/json",
"Authorization": "Bearer <Bearer token>",
"Client-Id": "<Client id>"
}
subscription_data = {
"type": "channel.follow",
"version": "2",
"condition":
{
"broadcaster_user_id": "<broadcaster id>",
"moderator_user_id": "<Moderator id>"
},
"transport":
{
"method": "websocket",
"session_id": session_id
}
}
subscription_data = json.dumps(subscription_data)
sleep(5)
post_response = requests.post("https://api.twitch.tv/helix/eventsub/subscriptions", headers=subscription_params, data=subscription_data)
r = post_response.json()
print(r)