Socket.io on backend and socket.io client on react front end issue
anybody here familiar with socket.io and socket.client??
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
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
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
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(...)
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?
you're using http
I’m confused 😭 that’s what was in the docs how can I have normal express app and add a socket.io to it 😭
no, to use websocket you have to use
ws
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
have you checked the examples in the website?