Skip to content
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

Couldn't establish connection between socket.io client (4.2.0) to socket server(4.2.0) #1495

Closed
5 tasks done
alpha-titan opened this issue Sep 21, 2021 · 8 comments
Closed
5 tasks done
Labels
bug Something isn't working
Milestone

Comments

@alpha-titan
Copy link

alpha-titan commented Sep 21, 2021

There are some bugs in the 4.2.0 client, I am not able to connect to the server.

I have enabled CORS on the socket server as well as the express server

I was stuck at this issue for hours tried everything no luck establish a WebSocket connection with the server, finally downgraded to 4.1.3

To reproduce

  • Create a React app with Vite js

  • npm init vite@latest react_app

  • install socket.io-client 4.2.0

  • Create an express server running on a different port

  • install socket.io 4.2.0

Client

Add the below code in app.jsx

const [socket, setSocket] = useState();
const handleSocket = () => {
        newSocket = io("ws://localhost:4000"); // HTTP didn't work either
	// newSocket.connect(); //? manual connection didn't work
	console.log(newSocket.connected); // false
        setSocket(newSocket); 
}
useEffect(()=>{
         handleSocket();
},[setSocket])

This exact code is working when downgrading the client socket version to 4.1.3

Server

const express = require("express");
const cors = require("cors");
const http = require("http");
const { Server } = require("socket.io");
const app = express();
app.use(cors());
const httpServer = http.createServer(app);
const io = new Server(httpServer, {
	cors: {
		origin: "*",
	},
});

io.on("connection", (serverSocket) => {
	serverSocket.emit("welcome", { data: "Welcome to electron chat" });
	//? broadcast the event to the namspace
	console.log(serverSocket.id);
	io.of("/").emit("messageFromServer", {
		message: `${serverSocket.id} has joined`,
	});
	//? listen for messageFromClient event in the main namespace
	serverSocket.on("disconnect", () => {
		console.log(`${socket.id} has disconnected`);
	});
       
});

httpServer.listen(4000, () => {
	console.log("Server running on 4000");
});


Upon inspecting Network for ws connections

using 4.1.3 connection is created

Screenshot 2021-09-20 at 11 11 07 PM

using 4.2.0 pending

Screenshot 2021-09-20 at 11 13 34 PM

when I use the CDN and comment out the import it works again.

@alpha-titan alpha-titan added the bug Something isn't working label Sep 21, 2021
@darrachequesne
Copy link
Member

I wasn't able to reproduce: https://github.com/socketio/socket.io-fiddle/tree/sample-react-app

Could you please check?

Note: could it be a problem of integrity here?

@darrachequesne darrachequesne added unable to reproduce and removed bug Something isn't working labels Sep 22, 2021
@swearl
Copy link

swearl commented Sep 23, 2021

same issue in vue 3.2

4.2 client cannot connect 4.2 server.
but 4.1.3 client can connect 4.2 server.

@darrachequesne
Copy link
Member

It seems there is actually a problem with some bundlers (see here), I'm digging into this.

@alpha-titan
Copy link
Author

Hi Sorry for the late reply,here is the link to my repo I made a sample app to reproduce the error

@darrachequesne
Copy link
Member

@alpha-titan thanks! I've suggested a temporary workaround here: vitejs/vite#4798 (comment)

@alpha-titan
Copy link
Author

Thanks @darrachequesne

darrachequesne added a commit to socketio/engine.io-client that referenced this issue Oct 14, 2021
It seems vite has issues with absolute dependencies in the "browser"
field, so we'll provide a quick workaround.

Related:

- socketio/socket.io-client#1494
- socketio/socket.io-client#1495
@Zefau
Copy link

Zefau commented Oct 16, 2021

I had the same issue.

Instead of attaching the httpServer directly via

const io = new Server(httpServer, {
	cors: {
		origin: "*",
	},
});

I'm using this now:

const io = new Server({
	cors: {
		origin: "*",
	},
});

io.attach(httpServer);

Worked for me for some reason. Hope it helps.

@darrachequesne
Copy link
Member

It should be fixed by this commit, included in version 4.3.x.

@darrachequesne darrachequesne added bug Something isn't working and removed unable to reproduce labels May 17, 2022
@darrachequesne darrachequesne added this to the 4.3.0 milestone May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants