Socket.io, express, P5 js project: server and client can't communicate

Hi, I’m just testing Glitch (which seems really great).
Like many, I’m starting with a small game based on Node.js, Express which uses socket.io for multiplayer features.
It’s a kind of distributed pacman very simplified (without walls and ghosts!)
As always, it works well on my LAN but I have some problems to make it work on glitch on the communication part between the client (web browser) and the node.js server

On the server side (server.js), here is how I initialize the listening socket:

var express = require("express");
var app = express();
var server = app.listen(process.env.PORT || 3000, "0.0.0.0", listen);

function listen() {
  var host = server.address().address;
  var port = server.address().port;
  console.log("Example app listening at http://" + host + ":" + port);
}

app.use(express.static("public"));

On the client side , the code that runs on the client browser (/public/sketch.js) here is how I connect to the server:

socket = io.connect("https://gamemanu.glitch.me:3000/");

From the point of view of the deployment and configuration files, I have this:

{
  "name": "simple io sockets p5",
  "version": "1.0.0",
  "description": "This is an example for how to use sockets with node.js and express and socket.io",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "author": "num_gg from Daniel Shiffman's courses",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "socket.io": "^4.2.0"
  }
}

On the logs, my server seems to be correctly listening on port 3000 (the listening message is displayed correctly) but nobody is connecting.

On the browser, the js app loads but doesn’t communicate on the socket at all. It sends nothing and receives nothing.

What am I missing to make it work?

Here’s the glitch project url: https://glitch.com/edit/#!/gamemanu

I suspect it is your use of ‘0.0.0.0’ rather than ‘127.0.0.1’.

From what I can tell it isn’t really starting up correctly. Maybe as you see things in logs but I believe the logs are simply echoing stuff. In other words it says “listening” because it was told to output that. :slight_smile:

The thing that concerned me was that I never saw the static page which seems to indicate it hasn’t quite started correctly.

I’ll admit there is some guessing on my part here but that is what I do when I have an issue.

Hi tleylan,

Thanks for your response. But when I wrote my post, the home page was working fine.

Now, the entire app seems broken. It cant’ even load socket.io package. :roll_eyes:

But this is nothing. I’ll ask my question differently: Is there somewhere on Glitch a project, an application posted by a user that implements Node.js with Express and that uses socket.io and that …works? :grinning:

That seems like it shouldn’t have the :3000 there. From the outside, Glitch serves your project on the usual port 443.

Hi wh0,
It’s no more a problem of tcp port. Now the app is down as it can’t even load express.js. It seems to be a configuration problem.

I think i’d better look at an application that is correctly deployed on Glitch. So my question is:

Is there somewhere on Glitch an application, a user-deployed project that uses Node, Express and Socket.io and that … works without a problem?

Could someone provide me with a link?

I can help!

I have a push-to-talk (PTT) system that uses these techs: https://glitch.com/edit/#!/sg-ptt

Also, here’s a thread of the last time someone was asking questions about getting websockets working (although their query was different): WebSocket not working

Let us know how it goes :slight_smile: