MCPcopy Create free account
hub / github.com/ConsortiumAI/Consortium / createSocket

Function createSocket

packages/consortium-web/src/lib/socket.ts:71–111  ·  view source on GitHub ↗
(
  token: string,
  callbacks: SocketCallbacks,
  scope:
    | { type: 'user-scoped' }
    | { type: 'session-scoped'; sessionId: string } = { type: 'user-scoped' },
)

Source from the content-addressed store, hash-verified

69}
70
71export function createSocket(
72 token: string,
73 callbacks: SocketCallbacks,
74 scope:
75 | { type: 'user-scoped' }
76 | { type: 'session-scoped'; sessionId: string } = { type: 'user-scoped' },
77): Socket {
78 const socket = io(SERVER_URL, {
79 auth: {
80 token,
81 clientType: scope.type,
82 ...(scope.type === 'session-scoped' ? { sessionId: scope.sessionId } : {}),
83 },
84 path: '/v1/updates',
85 transports: ['websocket', 'polling'],
86 reconnection: true,
87 reconnectionDelay: 1000,
88 reconnectionDelayMax: 5000,
89 reconnectionAttempts: Infinity,
90 });
91
92 socket.on('connect', () => {
93 callbacks.onConnect?.();
94 });
95
96 socket.on('disconnect', (reason) => {
97 callbacks.onDisconnect?.(reason);
98 });
99
100 socket.on('update', (data: { body?: Record<string, unknown> }) => {
101 if (data.body && typeof data.body.t === 'string') {
102 callbacks.onUpdate?.({ ...data.body, type: data.body.t } as unknown as UpdateEvent);
103 }
104 });
105
106 socket.on('ephemeral', (data: EphemeralEvent) => {
107 callbacks.onEphemeral?.(data);
108 });
109
110 return socket;
111}
112
113/**
114 * Send an encrypted message to a session via socket

Callers 2

SessionFunction · 0.90
HomeFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected