Giter VIP home page Giter VIP logo

Comments (12)

smeijer avatar smeijer commented on June 7, 2024 2

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.

rnenjoy avatar rnenjoy commented on June 7, 2024

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.

smeijer avatar smeijer commented on June 7, 2024

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.

rnenjoy avatar rnenjoy commented on June 7, 2024

Yeah exactly. I'm not qualified to answer that myself i'm afraid :)

from graphql-subscriptions.

corysimmons avatar corysimmons commented on June 7, 2024

https://www.youtube.com/watch?v=b2F-DItXtZs :o

from graphql-subscriptions.

smeijer avatar smeijer commented on June 7, 2024

I'm not seeing how that video is relevant.

from graphql-subscriptions.

rnenjoy avatar rnenjoy commented on June 7, 2024

Rethinkdb is very good at scaling so the changefeed part should be ok.

from graphql-subscriptions.

corysimmons avatar corysimmons commented on June 7, 2024

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

rnenjoy avatar rnenjoy commented on June 7, 2024

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.

rnenjoy avatar rnenjoy commented on June 7, 2024

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.

agborkowski avatar agborkowski commented on June 7, 2024

@rnenjoy where u store ( ActiveFeeds) ??

 // Add the feed cursor to the activeFeeds grouped on user uuid
 ActiveFeeds.add(context.uuid, feed)

from graphql-subscriptions.

grantwwu avatar grantwwu commented on June 7, 2024

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)

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.