DjangoChannelsGraphqlWs icon indicating copy to clipboard operation
DjangoChannelsGraphqlWs copied to clipboard

Send payload after initial connection

Open meshelton opened this issue 4 years ago • 3 comments

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.

meshelton avatar Feb 07 '21 16:02 meshelton

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

prokher avatar Feb 11 '21 22:02 prokher

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.

meshelton avatar Feb 12 '21 15:02 meshelton

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.

chidimo avatar Mar 02 '21 14:03 chidimo