graphql-mqtt-subscriptions icon indicating copy to clipboard operation
graphql-mqtt-subscriptions copied to clipboard

Retain retained messages and send them to new subscriptions

Open aullman opened this issue 4 years ago • 2 comments

Not sure how you feel about this. I had an issue where I wanted some messages to be retained but any new subscriptions were not getting them because we don't actually create a new subscription for every graphql subscription. This change is keeping the retained messages locally and sending them out to any new graphql subscriptions. We clean up the retained messages when there are no more subscriptions for that topic.

Would love to know if there is a better way to do this...

aullman avatar Sep 04 '20 06:09 aullman

Hey @aullman! Thanks for reaching out. I think sending retained information should be handled with GraphQL queries rather than using something like graphql-subscriptions... Could you elaborate more on your use case so that I'd understand why do you need the data to be sent this way?

davidyaha avatar Sep 04 '20 09:09 davidyaha

Sure, I'm building presence using graphql. Basically storing a lastSeen value for each user every few seconds and then having everyone subscribe to that. Rather than persisting this value I was just using retained mqtt messages so that any new person that comes online and misses an update will get the last one sent by that user. I expected that if I made messages retained then each time I have a new graphql subscription it will give me back the last retained message straight away. This works fine if you are the first person to subscribe to someone's presence but subsequent subscribes are not actually a new mqtt subscribe so mqtt does not send us the retained message. So this change is simulating that for any subsequent subscribes.

I suppose I could do this in my own code instead, but I would have to know whether there was an existing mqtt subscription and make sure I send the update to only that graphql subscription somehow.

aullman avatar Sep 04 '20 23:09 aullman