4

Im currently evaluating AWS AppSync as a backend solution for a messaging app.

The users will have a view to explore new Chat Groups and a different view where they see a list of their joined and private Chats (In the list the name and the last message of the chats should be displayed). Each Chat will of course have a detail view where all the messages are displayed.

The question is how to design the subscription part. My Mutation to send a message will look something like this:

createMessage( content: String, conversationId: ID!, createdAt: String!, id: ID! ): Message

According to the subscription docs i only have two possibilities to design my subscription. Either i subscribe to all new messages or to all new messages from a specific conversation by using the conversation id as an argument. So in my case i would need to fetch all the user conversations and then make a subscription call for every single conversation. Somehow this feels like a problem, but I don't see a different way (e.g. Custom filtering is not possible currently (according to this link))

Is there a better way to subscribe to new messages for a specific subset of messages (only in conversations im subscribed to)? Is having potentially 100s of active subscriptions on the client a problem?

Thanks in advance Luca

Luca
  • 132
  • 1
  • 7

1 Answers1

3

You are correct. The only two ways to do this out of the box is to:

  1. Subscribe to each conversation using an argument.
  2. Subscribe to all conversations and filter messages on the client.

If you subscribe to each conversation using an argument (option #1), you can batch send the subscribe requests in one HTTP request. E.g. Send up to 50 subscriptions with different conversation arguments in one request.

There is a third option, where you can do more work to ensure client efficiency. This option involves setting up a reverse index of conversations to client.

  1. Create an index where you can find clients given a conversation. The client will make one subscription with one argument (probably a client id). When you publish messages, you have an intermediate step (probably a backend job which is subscribed to all messages) where you look in the index to determine which clients are interested in the conversation you are publishing a message for. Then publish for each client.
  • Thanks a lot Micheal, I will take a look into the second and third option.Regarding option 3: I don't how the accomplish the "publish for each client" part. Let's assume my backend job would be a lamda function consuming all messages, then filtering the right clients (using the client id index) and publish. How would i code the publish part? – Luca Sep 10 '18 at 18:21
  • 1
    If you were using a lambda function the lambda function would do the following: 1) Subscribe to all new messages 2) For each new message: 2.1) Determine which conversation that message was for 2.2) Lookup which clients are interested in that message 2.3) For each interested client, publish the new message To implement the publish part to individual clients, you would be sending a mutation to AppSync for each client. Something like: mutation { publishMessage(client: $clientId) { yourMessage } } – Michael Willingham Sep 10 '18 at 18:56
  • Ok, got it. Since writing a single message could lead to let's say 100 Mutations (if there are 100s user in a conversations) i'll experiment a bit with option 1 and maybe just go with polling for my list views und subscribe to a specific chat only on Detail to be more cost efficient. Thanks again for your help Michael, really appreciate it! – Luca Sep 10 '18 at 20:43