lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Provide event to hook when client joins a subscription channel

Open GregPeden opened this issue 3 years ago • 2 comments

What problem does this feature proposal attempt to solve?

There's not a clear method provided to hook when a user joins a Lighthouse/GraphQL subscription channel. There are a variety of cases where this would be useful. As one example, in some use cases a race condition can exist where the client subscribes to a channel at the same time that some other job action is queued, meant to update the client as the job progresses. If the job is completed before the subscription channel can be authenticated, the client could miss a status update event. This can be solved by submitting a broadcast package immediately to users as they join the channel, but there is presently not a method provided to achieve this.

Which possible solutions should be considered?

Provide an event for when a user is authenticated on a broadcast channel which can be processed via "encodeTopic" and "filter" methods similar to the broadcast event structure, then leave it to developer to decide how to handle such cases. In the race condition case, a broadcast event could then be fired within which targets the newly joined user with a broadcast event containing the current status of the job. In other cases, this could be used to broadcast to other users when a user joins a channel (similar to what a Laravel Echo presence channel does automatically, except GraphQL/Lighthouse requires a different approach more like the above).

Unfortunately GraphQL clients like Apollo also don't provide a solution for hooking to subscription events to follow up with a good ol' API query to update the state, though this solution would itself create another less risky race condition.

GregPeden avatar Nov 19 '20 21:11 GregPeden

Seems reasonable, open for PRs.

spawnia avatar Nov 20 '20 12:11 spawnia

I'm considering contributing a PR for this. I am just studying the code base to see if I think I am up to tackling this.

GregPeden avatar Apr 14 '21 09:04 GregPeden