Gateway: allow addressing dispatch events to specific sessions

This commit is contained in:
Rory&
2026-03-13 03:24:12 +01:00
parent b17f3a3832
commit bf778e5133
3 changed files with 5 additions and 3 deletions

View File

@@ -106,6 +106,7 @@ export async function setupListener(this: WebSocket) {
}
this.events[this.user_id] = await listenEvent(this.user_id, consumer, opts);
this.events[this.session_id] = await listenEvent(this.session_id, consumer, opts);
await Promise.all(
relationships.map(async (relationship) => {

View File

@@ -36,6 +36,7 @@ import {
ReadyPrivateChannel,
GuildOrUnavailable,
Snowflake,
ThreadMember,
} from "@spacebar/util";
import { JsonValue } from "@protobuf-ts/runtime";
import {
@@ -49,11 +50,11 @@ import {
RelationshipType,
UserPrivate,
} from "@spacebar/schemas";
import { ThreadMember } from "../entities/ThreadMember";
export interface Event {
guild_id?: string;
user_id?: string;
session_id?: string;
channel_id?: string;
created_at?: Date;
event: EVENT;

View File

@@ -35,7 +35,7 @@ let unixSocketListener: UnixSocketListener | null = null;
let unixSocketWriter: UnixSocketWriter | null = null;
export async function emitEvent(payload: Omit<Event, "created_at">) {
const id = (payload.guild_id || payload.channel_id || payload.user_id) as string;
const id = (payload.guild_id || payload.channel_id || payload.user_id || payload.session_id) as string;
if (!id) return console.error("event doesn't contain any id", payload);
if (RabbitMQ.connection) {
@@ -485,7 +485,7 @@ class UnixSocketWriter {
await this.broadcastLock;
return await (this.broadcastLock = new Promise((res) => {
const tsw = Stopwatch.startNew();
const payloadBuf = Buffer.from(JSON.stringify({ id: (event.guild_id || event.channel_id || event.user_id) as string, event }));
const payloadBuf = Buffer.from(JSON.stringify({ id: (event.guild_id || event.channel_id || event.user_id || event.session_id) as string, event }));
const lenBuf = Buffer.alloc(4);
lenBuf.writeUInt32BE(payloadBuf.length, 0);
const framed = Buffer.concat([lenBuf, payloadBuf]);