Node.js Socket.io Error when trying to connect client to server

Greetings all,

I am trying to create a Web chat and to do this 1st we need to set up a client to server connection via Node and Socket.io - and it is not working.

The server code is here:
https://collectiveboost.com/cb_node/chat_app/server.js

And it runs fine when started via: node server.js
printing results to putty command line of: “Socket io Server Chat Started On V4 3000”

But on client side the socket.io connection is not happening :frowning:
And we have tried in both ways as stated by https://socket.io/ Get Started, which are:

via io global socket.io as you can see here:
https://collectiveboost.com/cb_node/chat_app/index.htm

this results in Error:
Failed to load resource: the server responded with a status of 404 (Not Found)
Uncaught Reference Error: io is not defined at (index):14

And via local version of the client-side JS file which can be reached via: node_modules/socket.io/client-dist/socket.io.js
as you can see here:
https://collectiveboost.com/cb_node/chat_app/index_2.htm

this results in Error:
polling-xhr.js:157 GET https://collectiveboost.com/socket.io/?EIO=4&transport=polling&t=Nt9UN_R 404 (Not Found)

So what are we doing wrong? How can we connect the client side Web page socket.io to server?

Thanks

The easiest solution is to use CDN version (which many of your clients will already have in cache) and manually specify connection URL and path. Don’t forget to add port (:3000) after the domain in URL.

URL

wss://collectiveboost.com:3000

Path

/socket.io/

What do you mean by " easiest solution is to use CDN version"?
If you mean to link to a 3rd party URL from client browser, for various reasons we cannot do this and all
recourses need to be served from our server.

So all I am asking Help with is why doesnt the client browser connect to the node.js based server via:

src=“/socket.io/socket.io.js”

and client throws the Errors:

Failed to load resource: the server responded with a status of 404 (Not Found)

Uncaught ReferenceError: io is not defined

Thank you Node.js GODs :slight_smile:

http://collectiveboost.com:3000/socket.io/socket.io.js

  1. Your server listens on port 3000
  2. SSL is not available at the moment, you have to manually specify paths to SSL-certificate files in your server code

Hello Veo,

I followed your instructions from that server code page, and created new server - here:

https://www.collectiveboost.com/cb_node/chat_app/server_ssl.js

the server starts OK with message to console of:

“Socket io Server Chat Started in SSL mode via https, on Port: 3000”

but again the client side it does not connect. And I tried both ways for:
https://www.collectiveboost.com/cb_node/chat_app/index.htm

src=“https://collectiveboost.com:3000/socket.io/socket.io.js
&
src=“/socket.io/socket.io.js”

What is going ON :frowning:
i really appreciate the support of Node.js GODs here to resolve this as it has been almost
5 days I have been trying to figure this out to get Socket.io between client and server established so we can move in with the main of this development.

Thanks again all

You missed the important part: listen() has to be called twice, on both server and io instances:

image

Try to add this line to the end of server_ssl.js:

io.listen(server);

I did your code, here:

https://collectiveboost.com/cb_node/chat_app/veo_server.js

and upon starting it, it throws this ERROR:

[root@mail chat_app]# node veo_server.js
/var/www/html/collectiveboost.com/cb_node/chat_app/veo_server.js:24
var io = require(‘socket.io’).listen(server);
^

TypeError: require(…).listen is not a function
at Object. (/var/www/html/collectiveboost.com/cb_node/chat_app/veo_server.js:24:31)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions…js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.