Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Server connection information Socket.io

It is necessary for my chat to display information (so far without nicknames) that the user has connected, implemented it this way, but for some reason it does not display anyway, what is the problem?

index.js

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const {Server} = require('socket.io');
const io = new Server(server);
io.on('connection', (socket) => {
    //console.log("a user connected");
   socket.on('connect', () => {
        io.emit('connect', "a user connected");
    });
    socket.on('chat message', (msg) => {
        io.emit('chat message', msg);
    });
    socket.on("disconnect", () => {
        console.log("a user disconnected");
    });
})

index.html

 let socket = io.connect("http://localhost:3000");
    let form = document.getElementById("form");
    let input = document.getElementById("input");
    form.addEventListener('submit', function(e) {
        e.preventDefault();
        if (input.value) {
            socket.emit('chat message', input.value);
            input.value = '';
        }
    });

    socket.on('chat message', (msg) => {
        let item = document.createElement('li');
        item.innerHTML = msg;
        messages.appendChild(item);
        window.scrollTo(0, document.body.scrollHeight);
    });

    let socket_connect = io.connect("http://localhost:3000");
    socket_connect.on('connect', (con) => {
        let item = document.createElement('li');
        item.innerHTML = con;
        connect.appendChild(item);
    });

The chat itself works well

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!