-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Uncaught TypeError: Illegal invocation #5308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi! I don't think this is necessary to recreate a new client, you can simply update the export const socketActions = {
connectMessageSocket(accessToken: string) {
if (socketStore.message.socket.connected || socketStore.message.connecting) return;
console.log('proceed to socket connection with token', accessToken)
socketStore.message.socket.auth.token = accessToken;
// [...]
socketStore.message.socket.connect();
},
// [...]
}; |
@darrachequesne Thank you for the suggestion. Unfortunately, that still produces |
Actually I tested that method before opening the issue here. Unfortunately that didn't worked. I'm really stuck on the middle of something due to this problem @darrachequesne. |
In more investigation i've found that I can't store the socket (result of Following is what I'm doing. // socketStore.ts (the store)
'use client';
import {proxy} from "valtio";
import {devtools, subscribeKey} from "valtio/utils";
import {Socket} from "socket.io-client";
type MsgModule = {
socket: null | Socket;
connecting: boolean;
}
type Data = {
message: MsgModule;
}
export const socketStore = proxy<Data>({
message: {
socket: null,
connecting: false,
},
});
devtools(socketStore, {
name: 'socket',
enabled: true,
});
export const socketActions = {
messageSocketSet(socket: Socket) { // just set the socket
socketStore.message.socket = socket;
},
};
subscribeKey(socketStore.message, 'socket', (sockConnected) => { // detect socket change
console.log('---------> socket updated', sockConnected)
}) The Conponent // ws.tsx
'use client'
import {io} from 'socket.io-client';
import {useEffect} from "react";
import {socketActions} from "@/store/socketStore";
const url = 'http://localhost:3000/message'
const ws = io(url, {
auth: {
token: ''
},
autoConnect: false
});
export default function WS() {
const connectHandler = () => {
console.log('connected')
socketActions.messageSocketSet(ws); // when connected, store the socket to use somewhere else [but this is what causing the error]
};
const connectErrHandler = (reason: unknown) => {
console.log('connect Err', reason);
};
const disconnectHandler = () => {
console.log('connect Err')
};
useEffect(() => {
ws.on('connect', connectHandler);
ws.on('connect_error', connectErrHandler);
ws.on('disconnect', disconnectHandler);
ws.auth = {token: 'the token string, assuming the token gets available later so we need to connect when it is available'};
ws.connect() // connect when we've the token
return () => {
ws.off('connect', connectHandler);
ws.off('connect_error', connectErrHandler);
ws.off('disconnect', disconnectHandler);
}
}, []);
return (
<h1>WS Comp</h1>
)
} To figure it out I've setup a fresh Next.js 15 project. Structure
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev --turbopack",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "15.2.1",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"socket.io-client": "^4.8.1",
"valtio": "^2.1.3"
},
"devDependencies": {
"@eslint/eslintrc": "^3",
"@tailwindcss/postcss": "^4",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"eslint": "^9",
"eslint-config-next": "15.2.1",
"tailwindcss": "^4",
"typescript": "^5"
}
} |
Describe the bug
It produces
Uncaught TypeError: Illegal invocation
error when callingio()
context: I'm working in a react project where valtio is used for state manamement.
To Reproduce
Socket.IO server version:
4.8.1
Server
Using Nest.js for server. The server is working fine.
Socket.IO client version:
4.8.1
Client
Expected behavior
Obviously expectation was to successfully connect, The official docs has a guide on how to use with react but that doesn't covers how to use with authentication when the token becomes available later.
Platform:
The text was updated successfully, but these errors were encountered: