#Return initial data when someone subscribes

8 messages · Page 1 of 1 (latest)

sand barn
#
 @Subscription(() => Message, {
    filter: (payload, variables) => {
      return payload.room === variables.room;
    },
    resolve: (payload) => {
      return payload.message;
    }
  })
  messageSent(@Args() _args: MessageSentArgs) {
    return this.redisService.pubsub.asyncIterator(MESSAGE_SENT_EVENT);
  }

Hi, I'd like to return the user like 100 recent messages on the given channel when he subscribes. How can I do it using graphql subscriptions in nestjs?

lapis pier
#

So, if the user enters (subscribes) to a room, they will enter the room via the UI, correct? This is what should trigger a GraphQL request/ query for the 100 last comments. The subscription request (to subscribe to a room) is just that and in fact, really shouldn't be a graphql subscription call. The subscription call should be to subscribe to getting new messages to the room, as they are made. See the difference?

sand barn
#

So the flow should be:

  1. Query the messages from this room
  2. Subscribe to new messages from this room
lapis pier
#

Um, maybe one step before that.

  1. Mutate to ask to enter a room. If yes...
  2. Let the user enter the room on the UI. This triggers the call to get the X last comments.
  3. Subscribe the user to get new comments.
#

GraphQL subscriptions are transferred via websockets or SSE, but, they aren't supposed to be used purely as websockets would be.

sand barn
#

I was wonderign if I can just do what I would normally do building a pure ws server.
Just onConnect return the latest messages and then onMessageSent add new ones

#

but aight

#

I will just query it then