Comments (10)
I spoke too soon... PR coming soon!
from graphql-ws.
Hey @MartijnHols and @brettjashford, I've added 2 new callbacks: onDisconnect
and onClose
. Both are used for tracking socket closures; however, they are a bit different:
onDisconnect
Called exclusively after the connection has been acknowledged (the client successfully went through the connection initialisation phase). Meaning, you can chain the onConnect
+ onDisconnect
to track actual, compatible, clients. onConnect
will always be called before onDisconnect
. Is called before onClose
.
onClose
Called when the socket closes at any point in time. It does not care if the connection has been acknowledged or not. The onConnect
callback must not necessarily be called before it. Is called after onDisconnect
.
from graphql-ws.
If the user disconnects abruptly without graphql-ws
acknowledging the connection, only onClose
will be called. So, yeah, I'd suggest using onClose
in your case.
from graphql-ws.
@enisdenjo Thanks for your help!
from graphql-ws.
i have yet to switch from subscriptions-transport-ws
, but an onDisconnect
server callback would be helpful. currently use this to send stats to datadog on the number of connected clients
from graphql-ws.
The thing is, the actual "server" is just a connection controller which can be hooked up to any WebSocket implementation. Its meant to be ultra minimal and its sole purpose is marshalling the messages and having high level control over the channel. Because of this, it does not have a concept of a "disconnect" - it simply does not care. So, ultimately, there is no place for a onDisconnect
in the controller.
However, you can indeed achieve this by providing a custom implementation over ws
using the raw import { makeServer } from 'graphql-ws'
or by simply hooking up on the onclose
event in the ws.Server
instance. You can check how I implement ws
here:
Lines 25 to 126 in 5483fac
If you need additional help, I'll write up an usage example for you!
from graphql-ws.
🎉 This issue has been resolved in version 4.0.0 🎉
The release is available on:
Your semantic-release bot 📦🚀
from graphql-ws.
Hi @enisdenjo thank you for the amazing library!
I would like to implement the same feature of what @MartijnHols said.
update the user's online status when a user disconnects
The code should be like this, right?
import { useServer } from 'graphql-ws/lib/use/ws';
import { db } from './my-db-service'
useServer({
context: (ctx) => ({
userId: ctx.connectionParams.userId // psuedo code, no auth logic
}),
onDisconnect: (ctx) => {
db.update({ status: 'offline' }).where({ userId: ctx.connectionParams.userId })
}
}, ws )
from graphql-ws.
Hey there @acro5piano! I'd recommend reading #91 (comment) to understand the difference between onClose
and onDisconnect
.
In essence, onDisconnect
is called only if the user successfully went through the connection phase (WebSocket established + ConnectionAck
message dispatched). On the other hand, onClose
is called regardless if the user successfully connected or not - so, every time the socket closes. The call order when the WebSocket closes is:
if acknowledged then onDisconnect
onClose
from graphql-ws.
@enisdenjo
Thank you for your quick response! So onClose
seems better because it is possible that user disconnects without sending signals, e.g.) internet disconnection.
from graphql-ws.
Related Issues (20)
- [ReactNative] Slow refresh subscription causes websocket close with {"isTrusted": false, "message": null} HOT 18
- Retry on different URL HOT 1
- Hasura connection error is not reported correctly
- Server race condition: connection ack + first subscribe HOT 6
- Allow to update connectionParams
- fix incorrect Bad Gateway error code HOT 3
- Subscriptions not working with Hasura via apollo Client graphql. error - Error: WebSocket implementation missing; HOT 1
- rename onMessage in server to postMessage, withMessage (or something more appropriate) HOT 1
- How to use `graphql-ws` protocol?
- Websocket using bun server - Unable to access request object in ctx.extras
- Warning Workspaces can only be enabled in private projects. HOT 3
- Detect subscription successfully started
- Errors during streaming subscriptions aren't properly handled or emitted HOT 1
- httpOnly cookie authentication
- Promises getting rejected with non-Error objects HOT 2
- Some subscriptions remain open after a websocket has been closed HOT 2
- Getting "Internal error occurred during message handling. Please check your implementation." after following recipe
- Clarify Contribution Policy
- Clients can be sent an `Error` message after cancellation. HOT 2
- How to avoid logging of hole object in graphql-ws?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-ws.