girayG
Convex Community2y ago
4 replies
giray

Chat App Bandwidth Problem

Hi, I am building a chat based multiplayer game. As you might guess, there can be many players and chat is being used a lot in the game. To get the chat I use the below query:

export const getAllForGame = query({
  args: {
    gameId: v.id("games"),
  },
  handler: async (ctx, args) => {
    const chat = await ctx.db
      .query("chat")
      .withIndex("by_gameId", (q) => q.eq("gameId", args.gameId))
      .order("desc")
      .take(50);
    return chat.reverse();
  },
});


The problem is that the game consumes a lot of bandwidth because of the chat and the problem is, a new action (new message or a reaction to any message) triggers a fetch, and I believe all clients who uses the above query, re-fetch the most recent 50 chat messages. Normally, I want to fetch all chat messages, I limited it to 50 because of this bandwidth problem.

I believe there should be a better way to fetch the chat messages for me. I have checked the paginated queries, however I could not figure out how to implement it for this problem. Mainly, I want to fetch all chat messages for a game, but I do not want it to re-fetch everything when a new message comes. (I believe Convex does not only send the changes but sends the result of the query as a whole). Convex caches the result of the query but the problem is not the cache, it is the bandwidth. I had to start a premium account even in the development stage and if I can not solve this problem, the game will not scale well.

For example, if I can paginate somehow and it seperates the whole chat into bundles of different queries than it will only consume bandwidth for small bundle changes. Any suggestions?
Was this page helpful?