Skip to content

Commit 66bd3f5

Browse files
committed
chore: bump version to 1.0.30 and refactor Socket.IO implementation in useChartSocket
1 parent 308d081 commit 66bd3f5

File tree

3 files changed

+59
-78
lines changed

3 files changed

+59
-78
lines changed

packages/oraidex-common-ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@oraichain/oraidex-common-ui",
3-
"version": "1.0.29",
3+
"version": "1.0.30",
44
"files": [
55
"dist/"
66
],

packages/oraidex-common-ui/src/components/TVChartContainer/helpers/useChartSocket.ts

Lines changed: 51 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -134,87 +134,68 @@ export const useChartSocket = ({
134134
}
135135
};
136136

137-
// Socket.IO implementation
137+
// Socket.IO implementation - Initialize socket
138138
useEffect(() => {
139+
// FIXME: currently: allway create instance of socket.io
139140
if (socketType === "socketio" && socketIOUrl) {
140-
const initSocketIO = async () => {
141-
try {
142-
// Clean up existing socket if any
143-
if (socketIORef.current) {
144-
socketIORef.current.off(eventName || "updateOhlcv", ohlcvHandler);
145-
socketIORef.current.disconnect();
146-
socketIORef.current = null;
147-
}
148-
149-
const socket = io(socketIOUrl, {
150-
transports: ["websocket", "polling"], // Fallback to polling if websocket fails
151-
autoConnect: false, // Don't auto connect, we'll connect manually
152-
reconnection: true,
153-
reconnectionAttempts: reconnectAttempts || WEBSOCKET_RECONNECT_ATTEMPTS,
154-
reconnectionDelay: reconnectInterval || WEBSOCKET_RECONNECT_INTERVAL,
155-
reconnectionDelayMax: 5000,
156-
timeout: 20000, // Increase timeout
157-
forceNew: true, // Force new connection
158-
...socketIOOptions
159-
});
160-
161-
socket.on("connect", () => {
162-
console.info("useChartSocket: Socket.IO connected - ", socketIOUrl);
163-
setIsConnected(true);
164-
});
165-
166-
socket.on("disconnect", (reason) => {
167-
console.info("useChartSocket: Socket.IO disconnected", reason);
168-
setIsConnected(false);
169-
});
170-
171-
socket.on("connect_error", (error) => {
172-
console.error("useChartSocket: Socket.IO connection error", error);
173-
setIsConnected(false);
174-
});
175-
176-
socket.on("reconnect_attempt", (attemptNumber) => {
177-
console.info(`useChartSocket: Socket.IO reconnection attempt ${attemptNumber}`);
178-
});
179-
180-
socket.on("reconnect_failed", () => {
181-
console.error("useChartSocket: Socket.IO reconnection failed");
182-
setIsConnected(false);
183-
});
184-
185-
socket.on("error", (error) => {
186-
console.error("useChartSocket: Socket.IO error", error);
187-
});
188-
189-
// Listen for ADL updateOhlcv events
190-
socket.on(eventName || "updateOhlcv", ohlcvHandler);
191-
192-
socketIORef.current = socket;
193-
194-
// Connect manually after setting up all event handlers
195-
socket.connect();
196-
} catch (error) {
197-
console.error("useChartSocket: Failed to initialize Socket.IO", error);
141+
const socket = io(socketIOUrl, {
142+
transports: ["websocket"],
143+
reconnectionAttempts: reconnectAttempts || WEBSOCKET_RECONNECT_ATTEMPTS,
144+
reconnectionDelay: reconnectInterval || WEBSOCKET_RECONNECT_INTERVAL,
145+
...socketIOOptions
146+
});
147+
148+
socket.on("connect", () => {
149+
console.info("useChartSocket: Socket.IO connected - ", socketIOUrl, socket.id);
150+
setIsConnected(true);
151+
});
152+
153+
socket.on("disconnect", (reason) => {
154+
console.info("useChartSocket: Socket.IO disconnected", reason, socket.id);
155+
setIsConnected(false);
156+
});
157+
158+
socket.on("connect_error", (error) => {
159+
if (socket.active) {
160+
// temporary failure, the socket will automatically try to reconnect
161+
console.warn("useChartSocket: Temporary connection error, attempting to reconnect...", error.message);
162+
} else {
163+
// the connection was denied by the server
164+
console.error("useChartSocket: Connection denied by server", error.message);
198165
setIsConnected(false);
199166
}
200-
};
167+
});
201168

202-
initSocketIO();
169+
socketIORef.current = socket;
203170

204171
return () => {
205172
if (socketIORef.current) {
206-
try {
207-
socketIORef.current.off(eventName || "updateOhlcv", ohlcvHandler);
208-
socketIORef.current.disconnect();
209-
socketIORef.current = null;
210-
setIsConnected(false);
211-
} catch (error) {
212-
console.error("useChartSocket: Error during cleanup", error);
213-
}
173+
socketIORef.current.off("connect");
174+
socketIORef.current.off("disconnect");
175+
socketIORef.current.off("connect_error");
176+
socketIORef.current.disconnect();
177+
socketIORef.current = null;
178+
setIsConnected(false);
214179
}
215180
};
216181
}
217-
}, [socketType, socketIOUrl, eventName, reconnectAttempts, reconnectInterval, socketIOOptions]);
182+
// FIXME: socketConfig,... is constant so no need add to dependency array
183+
// }, [socketType, socketIOUrl, reconnectAttempts, reconnectInterval, socketIOOptions]);
184+
}, []);
185+
186+
// Socket.IO implementation - Handle events
187+
useEffect(() => {
188+
if (socketType === "socketio" && socketIORef.current) {
189+
const socket = socketIORef.current;
190+
191+
// Listen for ADL updateOhlcv events
192+
socket.on(eventName || "updateOhlcv", ohlcvHandler);
193+
194+
return () => {
195+
socket.off(eventName || "updateOhlcv", ohlcvHandler);
196+
};
197+
}
198+
}, [socketType, eventName]);
218199

219200
// WebSocket subscription logic
220201
useEffect(() => {

packages/oraidex-common-ui/webpack.config.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ module.exports = {
4848
amd: "react-dom",
4949
root: "ReactDOM",
5050
umd: "react-dom"
51-
},
52-
"socket.io-client": {
53-
commonjs: "socket.io-client",
54-
commonjs2: "socket.io-client",
55-
amd: "socket.io-client",
56-
root: "io",
57-
umd: "socket.io-client"
5851
}
52+
// "socket.io-client": {
53+
// commonjs: "socket.io-client",
54+
// commonjs2: "socket.io-client",
55+
// amd: "socket.io-client",
56+
// root: "io",
57+
// umd: "socket.io-client"
58+
// }
5959
// "react-use-websocket": {
6060
// commonjs: "react-use-websocket",
6161
// commonjs2: "react-use-websocket",

0 commit comments

Comments
 (0)