Socket.io chat app doesn't work!

Hello!
Well, I created a chat application using NodeJS and Socket-io [express and ejs also, if that matters]. And it works just fine on my dev computer, but then I uploaded it onto Github and created a project from it on Glitch for hosting it. But the thing is, it doesn’t work.

By “it doesn’t work”, I mean, the server works, EJS properly renders my pages. But the actual chat messages don’t show up. Also, whenever I try to create a new room, it shows up only after I refresh the page. So I guess something is wrong with socket-io.

The site is live at chat-on-now.glitch.me, and the source code is at https://glitch.com/edit/#!/chat-on-now

Please help!

Are you sure your Javascript code is OK?

Make sure you loaded the Socket.io script at /socket.io/socket.io.js

Npm packages are not stored in the project. Instead, use the one from jscdn.

if you “wrapped” your app variable with socketio it can be accessed through yonle’s url.

Hi nikhilmwarrier,
The problem is in your 1st line of script.js in the Public directory.

change

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

to

const socket = io(“https://chat-on-now.glitch.me”); // glitch URL

Thanks

1 Like

What would work better is:

const socket = io(location.host);

This value gives you the hostname and port the page is at.

(psst… also you both had smart quotes instead of regular quotes! the code won’t work!)

1 Like

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