# gql.transport.websockets

class gql.transport.websockets.WebsocketsTransport(url: str, *, headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | SupportsKeysAndGetItem | None = None, ssl: SSLContext | bool = False, init_payload: Dict[str, Any] | None = None, connect_timeout: int | float | None = 10, close_timeout: int | float | None = 10, ack_timeout: int | float | None = 10, keep_alive_timeout: int | float | None = None, ping_interval: int | float | None = None, pong_timeout: int | float | None = None, answer_pings: bool = True, connect_args: Dict[str, Any] | None = None, subprotocols: List[str] | None = None)

  Bases: `WebsocketsProtocolTransportBase`

  Async Transport used to execute GraphQL queries on remote servers with websocket connection.

  This transport uses asyncio and the websockets library in order to send requests on a websocket connection.

  __init__(url: str, *, headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | SupportsKeysAndGetItem | None = None, ssl: SSLContext | bool = False, init_payload: Dict[str, Any] | None = None, connect_timeout: int | float | None = 10, close_timeout: int | float | None = 10, ack_timeout: int | float | None = 10, keep_alive_timeout: int | float | None = None, ping_interval: int | float | None = None, pong_timeout: int | float | None = None, answer_pings: bool = True, connect_args: Dict[str, Any] | None = None, subprotocols: List[str] | None = None) → None

    Initialize the transport with the given parameters.

    Parameters:

      - url – The GraphQL server URL. Example: ‘wss://server.com:PORT/graphql’.

      - headers – Dict of HTTP Headers.

      - ssl – ssl_context of the connection. Use ssl=False to disable encryption

      - init_payload – Dict of the payload sent in the connection_init message.

      - connect_timeout – Timeout in seconds for the establishment of the websocket connection. If None is provided this will wait forever.

      - close_timeout – Timeout in seconds for the close. If None is provided this will wait forever.

      - ack_timeout – Timeout in seconds to wait for the connection_ack message from the server. If None is provided this will wait forever.

      - keep_alive_timeout – Optional Timeout in seconds to receive a sign of liveness from the server.

      - ping_interval – Delay in seconds between pings sent by the client to the backend for the graphql-ws protocol. None (by default) means that we don’t send pings. Note: there are also pings sent by the underlying websockets protocol. See the keepalive documentation for more information about this.

      - pong_timeout – Delay in seconds to receive a pong from the backend after we sent a ping (only for the graphql-ws protocol). By default equal to half of the ping_interval.

      - answer_pings – Whether the client answers the pings from the backend (for the graphql-ws protocol). By default: True

      - connect_args – Other parameters forwarded to websockets.connect

      - subprotocols – list of subprotocols sent to the backend in the ‘subprotocols’ http header. By default: both apollo and graphql-ws subprotocols.

  adapter: WebSocketsAdapter
  property headers: Headers | Mapping[str, str] | Iterable[tuple[str, str]] | SupportsKeysAndGetItem | None
  property ssl: SSLContext | bool
  APOLLO_SUBPROTOCOL = 'graphql-ws'
  GRAPHQLWS_SUBPROTOCOL = 'graphql-transport-ws'
  async close() → None

    Coroutine used to Close an established connection

  async connect() → None

    Coroutine which will:

    - connect to the websocket address

    - send the init message

    - wait for the connection acknowledge from the server

    - create an asyncio task which will be used to receive and parse the answers

    Should be cleaned with a call to the close coroutine

  property connect_args: Dict[str, Any]
  async execute(request: GraphQLRequest) → ExecutionResult

    Execute the provided request against the configured remote server using the current session.

    Send a query but close the async generator as soon as we have the first answer.

    The result is sent as an ExecutionResult object.

  async execute_batch(reqs: List[GraphQLRequest], *args: Any, **kwargs: Any) → List[ExecutionResult]

    Execute multiple GraphQL requests in a batch.

    Execute the provided requests for either a remote or local GraphQL Schema.

    Parameters:

      reqs – GraphQL requests as a list of GraphQLRequest objects.

    Returns:

      a list of ExecutionResult objects

  property response_headers: Dict[str, str]
  async send_ping(payload: Any | None = None) → None

    Send a ping message for the graphql-ws protocol

  async send_pong(payload: Any | None = None) → None

    Send a pong message for the graphql-ws protocol

  async subscribe(request: GraphQLRequest, *, send_stop: bool | None = True) → AsyncGenerator[ExecutionResult, None]

    Send a query and receive the results using a python async generator.

    The query can be a graphql query, mutation or subscription.

    The results are sent as an ExecutionResult object.

  property url: str
  async wait_closed() → None
  init_payload: Dict[str, Any]
  ack_timeout: int | float | None
  payloads: Dict[str, Any]

    payloads is a dict which will contain the payloads received for example with the graphql-ws protocol: ‘ping’, ‘pong’, ‘connection_ack’

  ping_interval: int | float | None
  pong_timeout: int | float | None
  answer_pings: bool
  send_ping_task: Future | None
  ping_received: Event

    ping_received is an asyncio Event which will fire each time a ping is received with the graphql-ws protocol

  pong_received: Event

    pong_received is an asyncio Event which will fire each time a pong is received with the graphql-ws protocol

  connect_timeout: int | float | None
  close_timeout: int | float | None
  keep_alive_timeout: int | float | None
  next_query_id: int
  listeners: Dict[int, ListenerQueue]
  receive_data_task: Future | None
  check_keep_alive_task: Future | None
  close_task: Future | None
  close_exception: Exception | None