#React useQuery stop receive new data for some users
5 messages · Page 1 of 1 (latest)
Hi @serene night. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
This sounds like you’re hitting Convex’s per-function read limits or scalability constraints in how your queries are written, which can cause some subscriptions to stop updating once the data set gets large.
From the docs:
- Each query and mutation has limits on how much data it can read (and write) in a single execution, to guarantee performance. [Query limits; Mutation limits]
- If a query scans too many documents (for example, via a full table scan or a broad filter), you can hit read/write limit errors, and Convex will stop that function. [Read/write errors]
- Filters like
.filter((q) => ...)can cause full table scans; with tens of thousands of documents this can easily run into limits. You should switch to indexed queries with.withIndex(...). [Filtering performance; Queries that scale]
For real‑time chat / messages specifically:
- If you have a query like “get all messages for a chat” that returns a large list, every change (new message, edit, delete) will cause Convex to recompute and resend the entire result set to each subscribed client. [GPT streaming list; Discord thread]
- With “THOUSANDS of messages” this becomes inefficient; the recommended pattern is:
- Add an index on
chatId(or similar) and query viawithIndexinstead offilter. [Discord thread] - Use pagination so each query only returns a page of messages, not the entire history. [Discord thread; Queries that scale]
- Add an index on
Example from the Discord thread (adapted):
// schema
chatMessages: defineTable({
chatId: v.id("chats"),
userId: v.id("users"),
content: v.string(),
}).index("by_chat_id", ["chatId"]);
// query
import { query, paginationOptsValidator } from "convex/server";
import { v } from "convex/values";
export const getPaginatedMessages = query({
args: {
chatId: v.id("chats"),
paginationOpts: paginationOptsValidator,
},
handler: async (ctx, { chatId, paginationOpts }) => {
const messagesQuery = ctx.db
.query("chatMessages")
.withIndex("by_chat_id", (q) => q.eq("chatId", chatId))
.order("desc");
const page = await messagesQuery.paginate(paginationOpts);
return page;
},
});
What I’d check in your app:
- Server logs for
Read/write limiterrors or similar when those users stop receiving updates. [Read/write errors] - Any queries that:
- Use
.filterinstead of.withIndex. - Return very large arrays (all messages, all users, etc.) without pagination.
- Use
- Whether you have any “presence” / heartbeat‑style data that updates very frequently and is read by many queries; that can cause frequent invalidation and recomputation. The “Queries that scale” article shows how to split frequently changing fields into separate documents to reduce this. [Caching optimization]