0

I'm completely new to socket.io and I'm trying to connect a client to the server.

This is app.js.

const socketio = require("socket.io");
const app = express();

app.set("view engine", "ejs");
app.use(express.static("public"));

app.get("/", (req, res) => {
  res.render("index");
});

const server = app.listen(process.env.PORT || 3000, () => {
  console.log("server is running...");
});

// Initialize socket for the server
const io = socketio(server);

io.on("connection", socket => {
  console.log("New user connected");
});

This is index.ejs.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Real time Chat App</title>

    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
    />
  </head>
  <body>
    <div class="container">
      <div class="title">
        <h3>Realtime Chat Room</h3>
      </div>

      <div class="card">
        <div class="card-header">Anonymous</div>
        <div class="card-body">
          <div class="input-group">
            <input
              type="text"
              class="form-control"
              id="username"
              placeholder="Change your username"
            />
            <div class="input-group-append">
              <button class="btn btn-warning" type="button" id="usernameBtn">
                Change
              </button>
            </div>
          </div>
        </div>

        <div class="message-box">
          <ul class="list-group list-group-flush" id="message-list"></ul>
          <div class="info"></div>
        </div>

        <div class="card-footer">
          <div class="input-group">
            <input
              type="text"
              class="form-control"
              id="message"
              placeholder="Send new message"
            />
            <div class="input-group-append">
              <button class="btn btn-success" type="button" id="messageBtn">
                Send
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <script src="https://cdn.socket.io/socket.io-1.0.0.js"></script>
    <script src="/js/chatroom.js"></script>
  </body>
</html>

This is chatroom.js.

(function connect() {
    let socket = io.connect("http://localhost:3000")
})();

The problem is I don't know why io.on("connection", socket => { console.log("New user connected"); }); not working here. Everything seems to be OK in Google Chrome console, but the message doesn't appear in VS code terminal when I go to localhost website. The terminal always looks like this when I refresh the page. vs code terminal

Is there anything wrong with my code?

Sai_Rung
  • 33
  • 1
  • 5

2 Answers2

0

I solved it already. I change <script src="https://cdn.socket.io/socket.io-1.0.0.js"></script> to <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.5/socket.io.js"></script> and still have no idea what the problem is ,but it works.

Sai_Rung
  • 33
  • 1
  • 5
0

I think it's a cors origin problem, use Firefox to see it in the console

And that't how you can resolve it : socket.io, 'Access-Control-Allow-Origin' error

  • Hi, a comment from the author says that there were no errors in console. So the problem isn’t it – pierpy Jan 07 '23 at 13:48
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/33578291) – node_modules Jan 10 '23 at 14:33