0

I try to make a client-server app with socket.io.

Socket.io server seems work but the client cannot connect to it. The error i get is about the '/socket.io/socket.io.js' like what it is unable to load it.

So here are my questions

  • is it mandatory to have server and client in the same folder as we can see in the official demo ?
  • can we make a nodejs socket.io server without express ?
LChampo
  • 77
  • 1
  • 8

3 Answers3

0

Depending on how your project is setup, you need to create 2 server files, 1 for the app, and one for the websockets, and every time a user opens the app it should open(and be told where to try and open the connection) a connection to the websockets server. On my websockets apps I have the app running on localhost:3000, and websockets server on localhost:3001 (and tell the app to look for a server on 3001), so really you don't need to have the server files in the same folder, they can be in 2 opposite ends of your computer, as long as the app points to the server, then your fine, once a connection has been opened, the websocket server will see the client, and it should work! Let me know if that make sense.

0
  1. No, you can download socket.io front-end lib from another sources, for example cdn. Be sure you installed the right version.

  2. Yes, you can make it without express. Express is just another option for creating an socket.io server.

For example, currently in my project that is written in another back-end node framework i'm using the code below to establish the socket.io server.

const io = require("socket.io")(2337); 
io.on("connection", socket =>
  // some code
)
ykit9
  • 483
  • 4
  • 7
0

I fixed the problem of websocket communication between my server and my client.

After inspected my html client file, i saw on the console this error message 'ReferenceError: io is not defined'.

I google that error and i found this.

LChampo
  • 77
  • 1
  • 8