I have an application with apollo-server that handles multiple client devices using apollo-client to establish websocket subscriptions.
- When a client connects to the server, "onConnected" hook is called as expected.
- When a client disconnects from the server, "onDisconnected" hook is called as expected.
For theses two events, I can update the related database according to device connection state.
The ApolloServer is configured to enable keepAlive for open subscriptions so the clients can detect that the server is down or unrecheable and this is working at the client level.
But I would like to be notified at the server level if a client becomes unrecheable (Ethernet cable disconnected for exemple) but if this occurs, I never get any information to be aware of this situation and I cannot update the related database to reflect the device connection state.
Is there a way to achieve this goal ?
Here is the code :
const server = new ApolloServer({
typeDefs: typeDefs,
schemaDirectives: {
role: RoleDirective
},
resolvers,
context: async ({req, connection}) => {
if (connection) {
console.log('CONNECT with connection')
return connection.context
} else if (req) {
console.log('CONNECT with request')
let token = req.headers.authorization || '';
return await createContextFromRequest(req, token)
} else {
console.log('CONNECT with no request OR connection\n')
}
},
subscriptions: {
keepAlive: 10000,
onConnect: async (connectionParams, websocket, context) => {
console.log('Websocket CONNECTED')
let token = connectionParams.authorization || ''
return await createContextFromRequest(context.request, token)
},
onDisconnect: (websocket, context) => {
console.log('Websocket DISCONNECTED\n')
}
}
})