Subscription Examples
Use GraphQL subscriptions for real-time user and group updates.
For the event model and payload schema, see:
- Connect to AppSync Subscriptions
- Subscription Event Format
- Subscription Event Schema
- Using Subscriptions
- Track Document and Recipient Lifecycle with Subscriptions
User Feed Subscription
subscription SubscribeUserFeed {
subscribeUserFeed {
userId
data
}
}
Group Feed Subscription
subscription SubscribeGroupFeed($groupId: String!) {
subscribeGroupFeed(groupId: $groupId) {
groupId
data
}
}
JavaScript Example
import { createClient } from 'graphql-ws';
import WebSocket from 'ws';
const client = createClient({
url: 'wss://graphql.uk.legalesign.com/graphql',
webSocketImpl: WebSocket,
connectionParams: {
Authorization: `Bearer ${accessToken}`,
},
});
const parseEnvelope = (payload) => {
const raw = payload?.data;
if (!raw) return null;
return typeof raw === 'string' ? JSON.parse(raw) : raw;
};
client.subscribe(
{
query: `
subscription SubscribeUserFeed {
subscribeUserFeed {
userId
data
}
}
`,
},
{
next: ({ data }) => {
const wrapper = data?.subscribeUserFeed;
const event = parseEnvelope(wrapper);
console.log('user event', {
deliveryUserId: wrapper?.userId,
eventId: event?.eventId,
category: event?.category,
event: event?.event,
payload: event?.data,
});
},
error: console.error,
complete: () => console.log('user feed closed'),
}
);
client.subscribe(
{
query: `
subscription SubscribeGroupFeed($groupId: String!) {
subscribeGroupFeed(groupId: $groupId) {
groupId
data
}
}
`,
variables: { groupId: 'your-group-id' },
},
{
next: ({ data }) => {
const wrapper = data?.subscribeGroupFeed;
const event = parseEnvelope(wrapper);
console.log('group event', {
deliveryGroupId: wrapper?.groupId,
eventId: event?.eventId,
category: event?.category,
event: event?.event,
payload: event?.data,
});
},
error: console.error,
complete: () => console.log('group feed closed'),
}
);