Coder Social home page Coder Social logo

Comments (6)

OmgImAlexis avatar OmgImAlexis commented on June 19, 2024 18

Here's a simple version of what's shown in @mwood23's link.

subscriptions: {
    onConnect: connectionParams => {
        const apiKey = connectionParams['x-api-key'];
        ensureApiKey(apiKey);

        const user = Users.findOne({apiKey}) || {name: 'guest'};

        log.info(`<ws> ${user.name} connected.`);

        return {
            user
        };
    },
    onDisconnect: async (_, context) => {
        const initialContext = await context.initPromise;
        log.info(`<ws> ${initialContext.user.name} disconnected.`);
    }
}

from subscriptions-transport-ws.

arendjr avatar arendjr commented on June 19, 2024 10

To add to this, I think it would be valuable if the context properties which are returned from onConnect() would be passed to onDisconnect() just like they are passed to the resolvers. This way, there would be one consistent way of tracking users/connections.

from subscriptions-transport-ws.

NeoPhi avatar NeoPhi commented on June 19, 2024 6

Using an external Map() would ensure that you don't accidentally overwrite any state on the webSocket instance. Basic pattern would be:

onConnect(connectionParams, webSocket) {
  clients.set(webSocket, {custom: 'data'});
}
onDisconnect(webSocket) {
  const data = clients.get(webSocket);
  // use data as needed
}

from subscriptions-transport-ws.

NeoPhi avatar NeoPhi commented on June 19, 2024

The same webSocket instance is passed to all of the on* callbacks. You could use that to track custom state in a Map() or possibly on the webSocket itself.

from subscriptions-transport-ws.

myyellowshoe avatar myyellowshoe commented on June 19, 2024

Yeah I noticed that, I was wondering if I could stick some data in there, was thinking it would probably be just easiest to do it that way. Is there a reason I'd use one or the other?
Also how might I add data to the websocket? Any articles/ reference code you might be able to point me to?

Thanks so much!

from subscriptions-transport-ws.

mwood23 avatar mwood23 commented on June 19, 2024

Here's a relevant thread on Apollo's Spectrum that has a code snippet on accessing onConnect()'s context in onDisconnect(). Appears to work for me!

from subscriptions-transport-ws.

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.