Socket.io on backend and socket.io client on react front end issue

// front end
import { io } from "socket.io-client";

const socket = io("http://localhost:3000");

socket.on("connect", () => {
console.log("Connected to the server with ID:", socket.id);
});

// Export the socket instance to use it in components
export default socket;
// front end
import { io } from "socket.io-client";

const socket = io("http://localhost:3000");

socket.on("connect", () => {
console.log("Connected to the server with ID:", socket.id);
});

// Export the socket instance to use it in components
export default socket;
anybody here familiar with socket.io and socket.client??
// backend
const app = express();
const server = createServer(app);
const io = new Server(server, { cors: { origin: "http://localhost:5178" } });

io.on("connection", (socket) => {
console.log("A user connected:", socket.id);
});

app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
export { server, app };


import { server } from "./app.js";
import "dotenv/config";
server.listen(process.env.PORT, () => console.log("listening on port 3000"));
// backend
const app = express();
const server = createServer(app);
const io = new Server(server, { cors: { origin: "http://localhost:5178" } });

io.on("connection", (socket) => {
console.log("A user connected:", socket.id);
});

app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
export { server, app };


import { server } from "./app.js";
import "dotenv/config";
server.listen(process.env.PORT, () => console.log("listening on port 3000"));
for some reason when i run both my server and front end i dont see any console logs 😦 Request URL: ws://localhost:5178/ Request Method: GET Status Code: 101 Switching Protocols this is what i see in the network tab my backend is running on port 3000 I’m new to socket.io and I’m not understanding why I’m not seeing my console logs on the front end or backend I’m not seeing any cors errors in the console either For some reason my network tab logs the above sending a request to a different port even tho my server is on port 3000 My app.js file contains the server creation and everything I’m exporting the server to server.js file where I’m listening to the app I did this as I was testing my express app using super test I’d love some guidance https://github.com/moahnaf11/Messaging-App-Backend Relevant field app.js and server.js
9 Replies
ἔρως
ἔρως6d ago
you are trying to use a websocket as a normal api socket.io is for websockets you see the get request because websockets start with a get request that responds with 101 and after the 101, the communication is fone through the websocket protocol, not http
chikanlegbees
chikanlegbeesOP5d ago
Ohh so umm how would I have to structure it ? If u don’t mind clarifying? I’m new to the socket.io and I’ve followed the docs but I’m not sure what I’m doing wrong I want to use it as a websocket
ἔρως
ἔρως5d ago
from what i understood, you just need to listen to port 3000 in the server and then just connect to ws://...:3000 from the client side on both sides you then can socket.emit(...) something that is listened to using socket.on(...)
chikanlegbees
chikanlegbeesOP5d ago
Isn’t that what I’m doing ? I’m literally connecting to port 3000 from the front end which is my server port but it keeps setting the request to 5178 which is my front end in network tab?
ἔρως
ἔρως5d ago
you're using http
chikanlegbees
chikanlegbeesOP5d ago
I’m confused 😭 that’s what was in the docs how can I have normal express app and add a socket.io to it 😭
ἔρως
ἔρως5d ago
no, to use websocket you have to use ws
chikanlegbees
chikanlegbeesOP5d ago
Could u please send me a resource if possible ? I didn’t find any of that in their docs Like a guide to follow would be great Rly sorry for bothering u but I’m rly confused What’s should the code look like for a connection? I think I got it my import on client was wrong
ἔρως
ἔρως5d ago
have you checked the examples in the website?
Want results from more Discord servers?
Add your server