I want to connect to the websocket. When I inspect the traffic between the client and the server I see that the first message is the handshake:
{"type": "connection_init",
"payload":
{
"accept-language": "en",
"ec-version": "5.1.88",
"referrer": URL,
}
}
Based on the format (the keys of the dict) I conclude that the websocket uses an Apollo websocket transport prototcol.
Next, I am following the websocket-example of gql's documentation.
import asyncio
import logging
from gql import gql, Client
from gql.transport.websockets import WebsocketsTransport
logging.basicConfig(level=logging.INFO)
async def main():
transport = WebsocketsTransport(url=URL,
init_payload={'accept-language': 'en',
'ec-version': '5.1.88'})
async with Client(transport=transport,
fetch_schema_from_transport=False) as session:
# do something
asyncio.run(main())
After reading more about the protocol here I still don't understand how I can send messages to the server within my Python-script
How do I send the the below message to the websocket?
{
"id": "1073897396",
"type": "start",
"operationName": operation,
"eventId": 488
}
The EuropeanTour website does not use the Apollo websocket protocol for its API so you cannot use the standard WebsocketTransport class.
What I found out:
connection_ackmessage after theconnection_init, it returns awsIdentitymessagerequest-full-subscriptionmessage, requiring the client to send the full subscription queryfull-subscriptionmessage containing the full requested query, including theoperationNameand a new fieldsubscriptionNamesubscriptionNameand the variables, from a json format with the dictionary keys sorted. It took me a while to figure it out as no error is generated if the id is not correctHere is a Chrome screenshot showing the
request-full-subscriptionmessages:You can then create your own transport by inheriting the
WebsocketTransportclass and making the necessary modifications.Here is an example of working code: