MCPcopy
hub / github.com/chartbrew/chartbrew / connect

Method connect

client/src/modules/socketClient.js:25–113  ·  view source on GitHub ↗

* Initialize socket connection with authentication * @param {number} userId - User ID * @param {number} teamId - Team ID * @returns {Promise }

(userId, teamId)

Source from the content-addressed store, hash-verified

23 * @returns {Promise<void>}
24 */
25 async connect(userId, teamId) {
26 // If already connected with same credentials, reuse connection
27 if (this.socket?.connected && this.isAuthenticated && this.userId === userId && this.teamId === teamId) {
28 return Promise.resolve();
29 }
30
31 // Disconnect existing socket if credentials changed
32 if (this.socket && (this.userId !== userId || this.teamId !== teamId)) {
33 this.disconnect();
34 }
35
36 this.userId = userId;
37 this.teamId = teamId;
38
39 return new Promise((resolve, reject) => {
40 const timeout = setTimeout(() => {
41 reject(new Error("Socket connection timeout"));
42 }, 10000);
43
44 this.socket = io(API_HOST, {
45 withCredentials: true,
46 transports: ["websocket", "polling"], // Allow fallback for production
47 reconnection: true,
48 reconnectionAttempts: 5,
49 reconnectionDelay: 1000,
50 reconnectionDelayMax: 5000,
51 timeout: 20000,
52 autoConnect: true,
53 path: "/socket.io",
54 upgrade: true,
55 rememberUpgrade: true,
56 forceNew: true,
57 });
58
59 // Handle connection
60 this.socket.on("connect", () => {
61 // Authenticate immediately after connect
62 this.socket.emit("authenticate", { token: getAuthToken(), teamId });
63 });
64
65 // Handle successful authentication
66 this.socket.once("authenticated", (data) => {
67 clearTimeout(timeout);
68 if (data?.success === false) {
69 reject(new Error(data.error || "Socket authentication failed"));
70 return;
71 }
72 this.isAuthenticated = true;
73 resolve();
74 });
75
76 // Handle connection errors
77 this.socket.on("connect_error", (error) => {
78 clearTimeout(timeout);
79 reject(error);
80 });
81
82 // Handle disconnection

Callers 1

initSocketFunction · 0.45

Calls 3

disconnectMethod · 0.95
getAuthTokenFunction · 0.90
onMethod · 0.80

Tested by

no test coverage detected