What are subscriptions for?
GraphQL subscriptions are a feature of the GraphQL query language that allows clients to receive real-time updates from a server when specific events or data changes occur. They are useful for building interactive and responsive applications, as they eliminate the need for clients to continuously poll the server for updates.
Subscriptions work by establishing a long-lived connection between the client and server, enabling the server to push data to the client as soon as it becomes available, making them a more efficient and scalable way to handle real-time data synchronization in applications.
The Cosmo Router comes with subscription support out of the box without any limitations. We support multiple different protocols to enable the most versatile and efficient real-time data synchronization for your applications: WebSockets, Server-Sent Events, and Multipart HTTP Protocol, among others.
- 
graphql-ws: (default) Coherent, zero-dependency, lazy, simple, GraphQL over WebSocket Protocol compliant server and client.
- 
Server-Sent Events (SSE): One-Way Messaging. Both with GETandPOSTrequests. Recommended for unidirectional updates. More resource efficient than WebSockets.
- 
Multipart HTTP: Multi-part GraphQL subscriptions allow data to be delivered in chunks over time, enabling efficient streaming of large or frequently updated data in real-time applications.
- 
subscriptions-transport-ws: Legacy transport to WebSocket-based GraphQL subscriptions. Please use graphql-wsorSSEif you have the choice.
- 
absinthe: Legacy transport for Clients that still use the Absinthe (Phoenix) protocol.
Subscription architecture with router, protocol translator, and multiplexer.
Multiplexing
Multiplexing connections between clients and backend services.
Whenever your router updates its config at runtime, it terminates all active subscriptions. Clients should be built in a way that reconnects automatically on disconnect.
Test WebSockets
Install wscat globally through npm:
Run the following steps in order:
# 1. Establish a secure WebSocket connection with our demo Router.
wscat -c wss://demo-router.fly.dev/graphql --subprotocol graphql-transport-ws
# 2. Paste the initial init message and press ENTER.
{"type":"connection_init","payload":{}}
# 3. Paste the GraphQL subscription and press ENTER to get updates.
{"id":"6e6f71d0-2729-4db5-baee-8b6fc88721a7","type":"subscribe","payload":{"query":"subscription {\n  currentTime {\n    unixTime\n  }\n}"}}
Using the “extensions” field
Cosmo Router allows you to leverage the “extensions” field of a GraphQL Request JSON to add additional meta information to the subscription message.
This is useful, e.g. if you’re trying to send an additional parameter, like a Bearer token with the initial payload. The feature in enabled by default, here’s an example on how to use it:
# 1. Establish a secure WebSocket connection with our demo Router.
wscat -c wss://demo-router.fly.dev/graphql --subprotocol graphql-transport-ws
# 2. Paste the initial init message and press ENTER.
{"type":"connection_init","payload":{}}
# 3. Paste the GraphQL subscription and press ENTER to get updates.
{"id":"1","type":"subscribe","payload":{"extensions":{"token":"asd"},"query":"subscription {\n  currentTime {\n    unixTime\n  }\n}"}}