Comments (12)
I understand. But I was actually serious. Because the current quick and easy ways to implement graphql subscriptions do not scale. When we want to install a scalable solution; we need stacks that include MQTT
or redis
.
If switching from mongo
(or sql
) to rethinkdb
means that subscriptions are automatically included in a scalable way; than I can imagine a lot of people are willing to drop mongo
+ redis
in favor for rethinkdb
.
It will definitely improve the developer (/devops) experience.
from graphql-subscriptions.
Ok
Here is a working design.
Server.js
export const subscriptionServer = SubscriptionServer.create(
{
schema,
execute,
subscribe,
onConnect: (connectionParams, webSocket) => {
const userid = uuid.v4()
webSocket.uuid = userid
return { uuid: userid }
},
onDisconnect: (websocket) => {
ActiveFeeds.close(websocket.uuid)
}
},
{
server: server,
path: '/graphql',
}
)
resolvers.js
Subscription: {
changefeedTest: {
subscribe: (root, {}, context, self) => {
// Create an unique feed id
const feedUuid = context.uuid + self.fieldName
// Setup the changefeed query
r.table('people').changes().run().then((feed) => {
feed.each((error, change) => {
// Publish to the unique feed id
pubsub.publish(feedUuid, { changefeedTest: change.new_val })
})
// Add the feed cursor to the activeFeeds grouped on user uuid
ActiveFeeds.add(context.uuid, feed)
})
// Return the async iterator with the unique feed id
return pubsub.asyncIterator(feedUuid)
}
}
}
from graphql-subscriptions.
Any thoughts of this approach? With this code I skip the generic pubsub topics and create a unique one for each subscription query against the DB. This way I think i'm certain that each user will just get the data it should have since they will only get the unique topic.
I think the most important question is; does it scale?
from graphql-subscriptions.
Yeah exactly. I'm not qualified to answer that myself i'm afraid :)
from graphql-subscriptions.
https://www.youtube.com/watch?v=b2F-DItXtZs :o
from graphql-subscriptions.
I'm not seeing how that video is relevant.
from graphql-subscriptions.
Rethinkdb is very good at scaling so the changefeed part should be ok.
from graphql-subscriptions.
@smeijer Sorry, anytime I see a modern DB + the words "web scale" my mind goes there. I thought you might've been joking with @rnenjoy
from graphql-subscriptions.
How do we know if it scales? What needs scaling? is the bottle neck the database? Rethinkdb says this about changefeeds:
"Changefeeds perform well as they scale, although they create extra intracluster messages in proportion to the number of servers with open feed connections on each write."
from graphql-subscriptions.
I made some changes to allow for one subscription to handle insert, update and delete. Which a subscription normally couldn't handle.
resolvers.js
subscribe: (root, {}, context, self) => {
// Create an unique feed id
const feedUuid = context.uuid + self.fieldName
// Setup the changefeed query
r.table('people').changes().run().then((feed) => {
feed.each((error, change) => {
// Publish to the unique feed id
pubsub.publish(feedUuid, { peopeSubscription: { people: change.new_val, operationType: getOperationType(change) } } )
})
// Add the feed cursor to the activeFeeds grouped on user uuid
ActiveFeeds.add(context.uuid, feed)
})
// Return the async iterator with the unique feed id
return pubsub.asyncIterator(feedUuid)
}
schema.js
type knxSwitchSubscription {
people: people!
operationType: String!
}
the operationType is a string with insert/update/delete. With rethinkdb the changes array is missing old_val with an insert, missing new_val with a delete but has both with an update. So thats how its knows.
from graphql-subscriptions.
@rnenjoy where u store ( ActiveFeeds) ??
// Add the feed cursor to the activeFeeds grouped on user uuid
ActiveFeeds.add(context.uuid, feed)
from graphql-subscriptions.
I'm not quite sure that this is really a GraphQL subscriptions question; the one thing I did see that might be relevant has to do with cleaning up resources after a disconnect. That's being discussed #143
from graphql-subscriptions.
Related Issues (20)
- Transfer maintenance and resurrect graphql-subscriptions HOT 9
- Weird Apollo server error-could not connect to endpoint
- Peer Dependency Mismatch HOT 5
- Can't use async/await with withFilter function HOT 2
- Cannot Install on Node 16 NPM 8 HOT 1
- Using pubsub in onConnect and onDisconnect
- Complete event is never fired when using withFilter & graphql-redis-subscriptions
- Expose a way to wait for async iterator to subscribe HOT 1
- WSS graphql subscripiton issue on Nodejs
- Websocket endpoint issue
- Ability to dynamically add/remove topics to/from asyncIterator
- Better error should be thrown if subscription ID is invalid
- Using directives with subscriptions HOT 1
- Subscription: make `Subscription` resolver independent of resolvers for `Create` and `Update` records
- reject subscription if already initialised
- Return type of pubsub.asynctIterator should include AsyncIterable HOT 2
- Who does the logic to add graphql spec fields on the payload like `__typename`?
- inside docker graphql-redis-subscriptions searches to connect to localhost HOT 2
- Nest JS: Object is possibly 'undefined' in GraphQL
- Can the pubsub-async-iterator properties and methods be protected instead of private?
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-subscriptions.