WebSocket Link
Use WebSocket connections for real-time GraphQL subscriptions.
Basic Usage
import { createClient, wsLink } from 'mearie';
export const client = createClient({
links: [wsLink({ url: 'wss://api.example.com/graphql' })],
});
Terminating Link
WebSocket Link is a terminating link - it must be the last link in your chain (or one of the last if you have multiple terminating links).
Configuration
URL
Specify the WebSocket endpoint:
export const client = createClient({
links: [wsLink({ url: 'wss://api.example.com/graphql' })],
});
Connection Parameters
Pass authentication or other connection parameters:
export const client = createClient({
links: [
wsLink({
url: 'wss://api.example.com/graphql',
connectionParams: {
authToken: localStorage.getItem('token'),
},
}),
],
});
Dynamic Connection Parameters
Use a function for dynamic values:
export const client = createClient({
links: [
wsLink({
url: 'wss://api.example.com/graphql',
connectionParams: () => ({
authToken: localStorage.getItem('token'),
}),
}),
],
});
How It Works
- Opens WebSocket connection to server
- Sends subscription operations over the connection
- Receives real-time updates as messages
- Automatically reconnects on connection loss
Common Patterns
With HTTP Fallback
export const client = createClient({
links: [
cacheLink(),
httpLink({ url: 'https://api.example.com/graphql' }),
wsLink({ url: 'wss://api.example.com/graphql' }),
],
});
WebSocket Link handles subscriptions, HTTP Link handles queries and mutations.
Lazy Connection
Only open connection when first subscription starts:
export const client = createClient({
links: [
wsLink({
url: 'wss://api.example.com/graphql',
lazy: true, // Default: false
}),
],
});
Keep-Alive
Send periodic ping messages to keep connection alive:
export const client = createClient({
links: [
wsLink({
url: 'wss://api.example.com/graphql',
keepAlive: 30000, // Ping every 30 seconds
}),
],
});
Automatic Reconnection
WebSocket Link automatically reconnects when the connection is lost:
export const client = createClient({
links: [
wsLink({
url: 'wss://api.example.com/graphql',
reconnect: true, // Default: true
reconnectAttempts: 5, // Default: Infinity
reconnectInterval: 1000, // Default: 1000ms
}),
],
});
Reconnection happens independently of Retry Link.
Link Chain Placement
Place wsLink at the end of your chain:
export const client = createClient({
links: [
retryLink(),
dedupLink(),
cacheLink(),
httpLink({ url: 'https://api.example.com/graphql' }),
wsLink({ url: 'wss://api.example.com/graphql' }), // Last (for subscriptions)
],
});
This ensures all non-terminating links execute before opening the WebSocket connection.
Next Steps
- SSE Link - Alternative using Server-Sent Events
- Subscriptions - Learn about real-time updates
- Links - Learn about the link system