Comments (15)
@Knaackee You don't need to worry about the SubscriptionManager anymore as the graphql engine uses the schema itself to handle subscriptions.
I would recommend heading over to subscriptions-transport-ws and read their docs https://github.com/apollographql/subscriptions-transport-ws#server
Does that help?
from graphql-redis-subscriptions.
Tried to make the flow more apparent:
Subscribe flow:
Browser --subscribe x--> WS server --> graphql.subscribe --> RedisPubSub.asyncItrable(x)
--SUBSCRIBE x--> Redis
Publish flow:
Browser --mutation x--> graphql.mutate --> RedisPubSub.publish(x) --PUBLISH x--> Redis
--> RedisPubSub.asyncIterator.next(x) --> graphql.subscribe.resolve(x) --> WS server
--> Browser
Does that makes sense? 🤔
I suggest reading the code of this package and of the subscriptions-transport-ws
package to get clearer understanding, but let me know if you have more questions.
from graphql-redis-subscriptions.
Hello @Knaackee!
The API is almost identical to using the Event Emitter PubSub instance that is provided on the graphql-subscriptions
package.
We do have examples on the test files:
Getting an AsyncIterator - https://github.com/davidyaha/graphql-redis-subscriptions/blob/master/src/test/tests.ts#L304-L306
Publishing an event - https://github.com/davidyaha/graphql-redis-subscriptions/blob/master/src/test/tests.ts#L331
And the rest of the usage is same as graphql-subscriptions: https://github.com/apollographql/graphql-subscriptions/blob/master/README.md#getting-started-with-your-first-subscription
If that helps you, please consider submitting a PR to the README with an up to date example.
Thanks!
from graphql-redis-subscriptions.
@Knaackee Gone ahead and done it myself, Thanks for the insight.
Please take a look on the updated readme for examples and LMK if that worked for you.
Closing for now as was fixed with dd70b23
from graphql-redis-subscriptions.
Thanks! But whats still not clear to me is how to set the the redis pubsub to the SubscriptionServer without the (gone) SubscriptionManager.
from graphql-redis-subscriptions.
Thank you very much!
But how does the SubscriptionServer knows about the messages pushed to the pubsub? The clients browser is connected to a websocket server > subscription server so how does the message come to the user ?
from graphql-redis-subscriptions.
Thank you very much for your help!
I have the connection between the browser and Web socket server established
as well as the resolver publishing to the Redis PubSub. But what's still unclear is how
I pass the PubSub to the SubscriptionServer.
Imagine the following scenario:
I have 3 Web servers with the front end, back end and the Web socket server. I have one Redis server. Now I publish to this server. No problem. But (at my understanding) at some point the SubscriptionServer needs to pick the published message up in order to deliver to the client.
For this to work, there must be a connection between the SubscriptionServer and the PubSub system.
If my guessing was right. How I initialize the SubscriptionServer with the Redis PubSub?
from graphql-redis-subscriptions.
Yes you are right and it has the connection through the schema object.
Consider this SubscriptionsServer:
const subscriptionServer = SubscriptionServer.create(
{
schema,
execute,
subscribe,
},
{
server: websocketServer,
path: '/graphql',
},
);
It get's the schema object as a parameter. The schema object has the connection to the redis through it's asyncIterator:
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
type Subscription {
somethingChanged: Result
}
type Result {
id: String
}
const SOMETHING_CHANGED_TOPIC = 'something_changed';
export const resolvers = {
Subscription: {
somethingChanged: {
subscribe: () => pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC), // When this is executed, the current server will subscribe to the redis topic and wait for events
},
},
}
Each of your servers will run this subscribe resolver whenever a client send a subscribe operation to the SubscriptionServer. When an event is published on redis, they will get it, run the rest of the resolvers and (e.g. the Result
type resolvers) and send the resulting message to the client.
Hope it helps :)
LMK how it goes.
from graphql-redis-subscriptions.
@davidyaha This is how my code looks like but it doesn't work. I see my published messages going from my resolver through the pubsub to the Redis server but not delivered to the browser. The browser is connected to Web socket server as well. I've noticed that I see an "info" log entry in the Redis monitor after I reload my graphiql window. So this should not the problem neither.
Again, thanks for your support!
from graphql-redis-subscriptions.
Do you see a SUBSCRIBE message on the redis monitor?
from graphql-redis-subscriptions.
No, i only get the following
1513766296.760226 [0 127.0.0.1:55881] "info"
1513766296.762157 [0 127.0.0.1:55880] "publish" "HEALTH_STATUS_REQUESTED" "{\"healthStatus\":{\"status\":\"ok Wed Dec 20 2017 11:38:16 GMT+0100 (CET)\"}}"
from graphql-redis-subscriptions.
Do the client send a subscribe query that fits the schema? Does it work with the regular pubsub from the graphql-subscriptions package?
from graphql-redis-subscriptions.
Yes, the client sends a correct request. I do it with Graphiql. I get "Your subscription data will appear here after server publication!".
But wait, I just debugged the subscribe resolver and noticed that he doesn't get called. I have to debug why that happens.
from graphql-redis-subscriptions.
Hi @davidyaha there are 4 different aspects:
- Server setup - on which you mentioned about using SubscriptionsServer. It is clear now.
- Client setup - Not clear what setup to use. I was using SubscriptionClient to connect it with my app. Documentation talks about SubscriptionManager being deprecated. What we should use instead?
- CreatingPubSub on Server - it is very much clear
- Using PubSub on server side to publish and subscribe - it is also clear
from graphql-redis-subscriptions.
Hey @jalalat! That's good to hear that points 1,3,4 are clearer now :)
About the client setup, that's actually not in the scope of this package at all.
That part is handled by the ws-transport package and apollo client. You can get more information on their readme: https://github.com/apollographql/subscriptions-transport-ws#client-browser
and docs: https://www.apollographql.com/docs/react/features/subscriptions.html#subscriptions-client
Hope that helps :)
from graphql-redis-subscriptions.
Related Issues (20)
- GraphQL v16 HOT 2
- Update ioredis HOT 1
- pub/sub type safety improvements
- Ability to dynamically add/remove topics to/from asyncIterator
- Fail to startup with IOREDIS v5.0.5 HOT 1
- When filtering on subscriptions, parameters are always undefined HOT 1
- ID of the payload returned by the subscription is always null | undefined HOT 1
- Getting Type 'Redis' is not assignable to type 'RedisClient' on v2.5.0 HOT 2
- issue with Redis , no construct
- Node Redis Support HOT 2
- pubsub.asyncIterator should return an AsyncIterable
- TS2322: Type 'Redis' is not assignable to type 'RedisClient'. HOT 6
- Support sharded pub/sub
- Error: Connection is closed. with pubsub HOT 1
- Race condition with simultaneous subscribe + quick unsubscribe HOT 9
- Please update Readme.md with required redis ACL perms for cluster HOT 1
- Callback when connection is closed?
- graphql-redis-subscriptions version 2.6.0 is not working with Node 16.0.0 HOT 1
- inside docker graphql-redis-subscriptions searches to connect to localhost HOT 2
- Simple example not working with ioredis 5 due to mismatched types HOT 1
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-redis-subscriptions.