Python: websocket transport session does not exist or has already disconnected - due to send

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)

well this won’t do anything anyway. You can’t control/specify the timeout

You can subscribe the moment the session ID is recieved in the welcome message.

So you have the same problem as OP or with different code getting problem?

Same problem as OP with similar but different code.
This is the response I get:

{'error': 'Bad Request', 'status': 400, 'message': 'websocket transport session does not exist or has already disconnected'}

It seems like I got this message because I was sending through the websocket which the connection does not allow.

await websocket.send(name)
print(f'client sent: {name}')

I found out by adding an error handler which gave me:

websockets.exceptions.ConnectionClosedError: received 4001 (private use) sent inbound traffic; then sent 4001 (private use) sent inbound traffic

Which you can see explained here:

After removing my send message I was able to successfully connect.

yeah you have a different problem to OP.

OP’s code is wrong and you are trying to create a subscription via the Websocket when a HTTP Request is required.

as I missed this section when scanning the code as you declared the same reason as OP so I jumped to whre there post request and sessionID was being read.

Split to new post for clarity

1 Like