RocketChat/Rocket.Chat · error · Error

error-invalid-room

error-invalid-room

Error message

error-invalid-room

What it means

loadMessageHistory (apps/meteor/server/lib/messages/loadMessageHistory.ts:29-33) resolves the room via Rooms.findOneById(rid) unless the caller passes a preloaded room object; if neither yields a room it throws a plain Error 'error-invalid-room'. This powers history loading (loadHistory and related loaders), and the rid comes straight from the requesting client.

Source

Thrown at apps/meteor/server/lib/messages/loadMessageHistory.ts:32

	ls,
	showThreadMessages = true,
	offset = 0,
	room: providedRoom,
}: {
	// userId is undefined if user is reading anonymously
	userId?: string;
	rid: string;
	end: Date | undefined;
	limit?: number;
	ls?: string | Date;
	showThreadMessages?: boolean;
	offset?: number;
	room?: IRoom;
}) {
	const room = providedRoom ?? (await Rooms.findOneById(rid, { projection: { sysMes: 1 } }));

	if (!room) {
		throw new Error('error-invalid-room');
	}

	const hiddenSystemMessages = settings.get<MessageTypesValues[]>('Hide_System_Messages');

	const hiddenMessageTypes = getHiddenSystemMessages(room, hiddenSystemMessages);

	const options: FindOptions<IMessage> = {
		sort: {
			ts: -1,
		},
		limit,
		skip: offset,
	};

	const records = end
		? await Messages.findVisibleByRoomIdBeforeTimestampNotContainingTypes(
				rid,
				end,

View on GitHub (pinned to b2c16d5842)

Solutions

  1. Verify the rid exists (Rooms.findOneById) or reuse the room document you already loaded and pass it as loadMessageHistory's room parameter
  2. Refresh/repair the client's room list when this fires - the id is stale
  3. Treat this as 'room gone': close the subscription and clean local caches instead of retrying the same rid
  4. Validate rid format (server-side ids are 17-char Mongo ObjectIds) before calling

Example fix

// before
const history = await loadMessageHistory({ rid, end, ls });

// after
const room = await Rooms.findOneById(rid);
if (!room) return handleRoomGone(rid);
const history = await loadMessageHistory({ rid, end, ls, room });
Defensive patterns

Strategy: validation

Validate before calling

const room = providedRoom ?? (await Rooms.findOneById(rid));
if (!room) return handleRoomGone(rid); // close subscription, clear caches
return loadMessageHistory({ rid, end, ls, room });

Try / catch

try {
  await loadMessageHistory({ rid, end, ls });
} catch (error: any) {
  if (error.message === 'error-invalid-room') {
    markRoomDeletedLocally(rid); // stale id; stop requesting history for it
    return;
  }
  throw error;
}

Prevention

When it happens

Trigger: Requesting history for a rid that does not exist (typo, stale id from another workspace); the room was deleted while the client still had it open; deep-linking to a room the server no longer has; passing an empty rid string.

Common situations: Clients with stale local storage after workspace resets/imports; rooms deleted by retention policies or moderators while users keep tabs open; copy-paste or URL-manipulation bugs producing malformed rids; test environments pointing at a different database.

Related errors


AI-assisted analysis of RocketChat/Rocket.Chat@b2c16d5842 (2026-08-18). Data as JSON: /api/errors/64bdc94b146bf97e. Report an issue: GitHub.