Code Monkey home page Code Monkey logo

Comments (15)

davidyaha avatar davidyaha commented on August 21, 2024 1

@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.

davidyaha avatar davidyaha commented on August 21, 2024 1

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.

davidyaha avatar davidyaha commented on August 21, 2024

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.

davidyaha avatar davidyaha commented on August 21, 2024

@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.

Knaackee avatar Knaackee commented on August 21, 2024

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.

Knaackee avatar Knaackee commented on August 21, 2024

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.

Knaackee avatar Knaackee commented on August 21, 2024

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.

davidyaha avatar davidyaha commented on August 21, 2024

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.

Knaackee avatar Knaackee commented on August 21, 2024

@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.

davidyaha avatar davidyaha commented on August 21, 2024

Do you see a SUBSCRIBE message on the redis monitor?

from graphql-redis-subscriptions.

Knaackee avatar Knaackee commented on August 21, 2024

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.

davidyaha avatar davidyaha commented on August 21, 2024

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.

Knaackee avatar Knaackee commented on August 21, 2024

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.

jalalat avatar jalalat commented on August 21, 2024

Hi @davidyaha there are 4 different aspects:

  1. Server setup - on which you mentioned about using SubscriptionsServer. It is clear now.
  2. 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?
  3. CreatingPubSub on Server - it is very much clear
  4. Using PubSub on server side to publish and subscribe - it is also clear

from graphql-redis-subscriptions.

davidyaha avatar davidyaha commented on August 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.