Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send payload after initial connection #63

Open
meshelton opened this issue Feb 7, 2021 · 3 comments
Open

Send payload after initial connection #63

meshelton opened this issue Feb 7, 2021 · 3 comments

Comments

@meshelton
Copy link

Is there a way to broadcast a message after the initial connection? Would love to sue the `subscribe method in my subscription but the groups haven't been set up at that point.

@prokher
Copy link
Member

prokher commented Feb 11, 2021

@meshelton What are you trying to achieve? What is the purpose?

@meshelton
Copy link
Author

Basically I'm using a subscription to complete some auth flow stuff so the user doesn't have to do any work after they complete an oauth flow. I'm new to relay/graphql so I'm not sure if this is best practices but my ideal approach would be:

  1. FE Page requiring auth token loads and sets up initial subscription to get auth token updates
  2. After receiving a connection the server immediately broadcasts an existing auth token if it has a valid one in the db
  3. FE Page gets the broadcasted event and then starts using the auth token
  4. Eventually the auth token expires and the FE page starts the auth flow again
  5. Auth flow redirects to a server route which stores the newly generated auth token and broadcasts the new auth token's availability
    and then 4 and 5 repeat until the user closes the page.

I haven't found a way to send the initial auth token in step 2 so the process currently looks like this:

  1. FE Page requiring auth token loads and sets up initial subscription to get auth token updates
  2. FE Page also runs a query to get an initial auth token from the server if it exists
  3. FE Page starts using the auth token from the regular query if it exists and is not expired. If it does not exist or is expired the FE goes into step 4
  4. Eventually the auth token expires and the FE page starts the auth flow again
  5. Auth flow redirects to a server route which stores the newly generated auth token and broadcasts the new auth token's availability

The current approach but it would be nice to remove that extra query, not strictly necessary just a nice to have.

@chidimo
Copy link

chidimo commented Mar 2, 2021

You can do this if you're using apollo client. So you just pass variables to the useSubscription hook as usual. This variable should be optional.
So each time this variable changes the subscription is stopped and re-run.

I currently use it in my web app that requires the logged in user's email. Initially the email is blank, but once the user logs in it re-runs the subscription with the now available email.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants