Skip to content

Commit 98d4d1a

Browse files
feat: implement lazy initialization of Socket.IO client
- Socket.IO client is now initialized lazily when first needed - Modified client.ts to use getSocket() function for deferred initialization - Updated agents module to call getSocket() in subscribeToConversation and addMessage - Socket connection is only established when these functions are actually called - Maintains backward compatibility while improving startup performance Co-authored-by: Netanel Gilad <netanelgilad@users.noreply.github.com>
1 parent 84de8a5 commit 98d4d1a

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

src/client.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,16 @@ export function createClient(config: {
5858
token,
5959
};
6060

61-
const socket = RoomsSocket({
62-
config: socketConfig,
63-
});
61+
let socket: ReturnType<typeof RoomsSocket> | null = null;
62+
63+
const getSocket = () => {
64+
if (!socket) {
65+
socket = RoomsSocket({
66+
config: socketConfig,
67+
});
68+
}
69+
return socket;
70+
};
6471

6572
const headers = {
6673
...optionalHeaders,
@@ -113,14 +120,16 @@ export function createClient(config: {
113120
functions: createFunctionsModule(functionsAxiosClient, appId),
114121
agents: createAgentsModule({
115122
axios: axiosClient,
116-
socket,
123+
getSocket,
117124
appId,
118125
serverUrl,
119126
token,
120127
}),
121128
appLogs: createAppLogsModule(axiosClient, appId),
122129
cleanup: () => {
123-
socket.disconnect();
130+
if (socket) {
131+
socket.disconnect();
132+
}
124133
},
125134
};
126135

@@ -132,14 +141,16 @@ export function createClient(config: {
132141
functions: createFunctionsModule(serviceRoleFunctionsAxiosClient, appId),
133142
agents: createAgentsModule({
134143
axios: serviceRoleAxiosClient,
135-
socket,
144+
getSocket,
136145
appId,
137146
serverUrl,
138147
token,
139148
}),
140149
appLogs: createAppLogsModule(serviceRoleAxiosClient, appId),
141150
cleanup: () => {
142-
socket.disconnect();
151+
if (socket) {
152+
socket.disconnect();
153+
}
143154
},
144155
};
145156

@@ -178,9 +189,12 @@ export function createClient(config: {
178189
*/
179190
setToken(newToken: string) {
180191
userModules.auth.setToken(newToken);
181-
socket.updateConfig({
182-
token: newToken,
183-
});
192+
if (socket) {
193+
socket.updateConfig({
194+
token: newToken,
195+
});
196+
}
197+
socketConfig.token = newToken;
184198
},
185199

186200
/**

src/modules/agents.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import { getAccessToken } from "../utils/auth-utils.js";
66

77
export type AgentsModuleConfig = {
88
axios: AxiosInstance;
9-
socket: ReturnType<typeof RoomsSocket>;
9+
getSocket: () => ReturnType<typeof RoomsSocket>;
1010
appId: string;
1111
serverUrl?: string;
1212
token?: string;
1313
};
1414

1515
export function createAgentsModule({
1616
axios,
17-
socket,
17+
getSocket,
1818
appId,
1919
serverUrl,
2020
token,
@@ -52,6 +52,7 @@ export function createAgentsModule({
5252
message: AgentMessage
5353
) => {
5454
const room = `/agent-conversations/${conversation.id}`;
55+
const socket = getSocket();
5556
await socket.updateModel(
5657
room,
5758
{
@@ -70,6 +71,7 @@ export function createAgentsModule({
7071
onUpdate?: (conversation: AgentConversation) => void
7172
) => {
7273
const room = `/agent-conversations/${conversationId}`;
74+
const socket = getSocket();
7375
return socket.subscribeToRoom(room, {
7476
connect: () => {},
7577
update_model: ({ data: jsonStr }) => {

0 commit comments

Comments
 (0)