Sunday 13 August 2023

How do I communicate using gql with Apollo websocket protocol

I want to connect to the websocket that is behind this website (click on a player so the golf course becomes visible). 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",
            "sport": "GOLF",
            "referrer": "https://www.europeantour.com/",
            "operator": "europeantour"
           }
}

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='wss://btec-websocket.services.imgarena.com',
                                    init_payload={'accept-language': 'en',
                                                  'ec-version': '5.1.88',
                                                  'operator': 'europeantour',
                                                  'referrer': 'https://www.europeantour.com/',
                                                  'sport': 'GOLF'})
    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": "SubscribeToGolfTournamentStatus",
 "eventId": 488
}


from How do I communicate using gql with Apollo websocket protocol

No comments:

Post a Comment