Giter Site home page Giter Site logo

Comments (14)

larixer avatar larixer commented on May 6, 2024

No, it is not obsolete. The subscriptions will stop working if you delete it.

from apollo-universal-starter-kit.

mitjade avatar mitjade commented on May 6, 2024

Opened 2 browsers and tried it out. Still working..

from apollo-universal-starter-kit.

larixer avatar larixer commented on May 6, 2024

@mitjade Yes you are right, actually subscriptions server is set up in api_server.js, so I think websockets_server.js is outdated indeed.

from apollo-universal-starter-kit.

mitjade avatar mitjade commented on May 6, 2024

Found this out by accident. Will create PR.

Am trying to add mqtt client so I would be able to publish mqtt events in mutations and still keep existing subscriptions, but can not seem to set it in a way to not have it restart on page reload or hot code reload, but still be able to expose client publish in schema.

I know this has nothing to do with this starter kit, but if you have any short pointers on how to archive this, would really appreciate it. If not np.

from apollo-universal-starter-kit.

larixer avatar larixer commented on May 6, 2024

@mitjade I have no idea. If you could show me the code, that reveals the problem with hot code reload, I might be able to look when I will have time. I believe even making subscriptions in this starter kit to hot reload code changes in counter mutation code without restarting subscriptions might not be possible. Generally subscriptions need to be made hot code reload friendly first.

from apollo-universal-starter-kit.

mitjade avatar mitjade commented on May 6, 2024

Thx. Will prepare something in a fork, if I will not be able to figure this out.

from apollo-universal-starter-kit.

mitjade avatar mitjade commented on May 6, 2024

https://github.com/mitjade/apollo-fullstack-starter-kit/commit/cc3e95e70b8ae4c9693fb117900f602278ee8026

Here is an example of what I'm trying accomplish.

Start an MQTT client and subscribe to specific topics, which I managed to get working.

What I am not able to do, is to export a publish function in src/server/api/mqtt_client.js that I could then call when a certain Mutation is triggered.

If I call it as demonstrated in src/server/api/schema.js, if you press the 'Click to increase count', I get an:
error { TypeError: _mqtt_client2.default.publish is not a function

I tried a few different approaches, all unsuccessful, so it looks like I still need to learn a few things on how javascript works...

from apollo-universal-starter-kit.

mitjade avatar mitjade commented on May 6, 2024

Another approach was:

src/server/api/schema.js

...
  Mutation: {
    addCount(_, { amount }, context) {
      const client = mqttClient();
      client.publish('topic', 'message');

Here I get:
error { TypeError: Cannot read property 'on' of undefined

from apollo-universal-starter-kit.

larixer avatar larixer commented on May 6, 2024

Yes, you are certainly doing something wrong, why your src/server/api/mqtt_client.js closure has parameter mqttClient, e.g. export default (mqttClient) =>, where do you pass this parameter to your closure? You don't pass it at all

from apollo-universal-starter-kit.

larixer avatar larixer commented on May 6, 2024

You should call your mqtt_client.js module like this:
mqttClient(realMqttClientInstance).publish('topic', 'message');

from apollo-universal-starter-kit.

mitjade avatar mitjade commented on May 6, 2024

How do I export the mqttClient instance from the src/server/api_server.js so I can access it in src/server/api/schema.js?

from apollo-universal-starter-kit.

mitjade avatar mitjade commented on May 6, 2024

export default mqttClient; in the src/server/api_server.js.

Sorry for a stupid question :) will try it out now.

from apollo-universal-starter-kit.

larixer avatar larixer commented on May 6, 2024

I believe the way to do it is to use Apollo GraphQL context, see file middleware/graphql.js you should pass an instance of your mqttClient as parameter there, e.g.

app.use('/graphql', (...args) => graphqlMiddleware(mqttClient)(...args));
export default mqttClient => graphqlExpress(() => {
  return {
    schema,
    context: {
      Count: new Count(),
      mqttClient: mqttClient
    },
  };
});

And then in your GraphQL schema you will be able to access mqttClient from context, the same way new Count() instance is accessed from context

from apollo-universal-starter-kit.

mitjade avatar mitjade commented on May 6, 2024

Managed to get it working!

Thank you AGAIN for the help!!! I would probably keep banging my head agains the wall with this problem for quite a while. I guess I need to read up on some javascript fundamentals :)

from apollo-universal-starter-kit.

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.