Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
Socket.io node server can't emit events to an electron app

I just followed the Get Started tutorial from https://socket.io/get-started/chat. It works with a web client on the browser. But it won't emit any event to a electron client. No error messages in any console. It just won't connect the socket in an electron app to the socket.io server.

The problem seems to be the io.emit() method, commenting that line makes possible for the electron client to successfully connect to the socket.io server.

This is the code of the nodejs server:

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);

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {

    console.log('a user connected');

    socket.on('chat message', (msg) => {
        //io.emit('chat message', msg);
        console.log(msg);
    });
    
});

server.listen(3000, () => {
    console.log('listening on *:3000');
});

This is the code of the preload.js of the electron app:

const io = require("socket.io-client");
const socket = io('http://localhost:3000');

socket.on('connect', () => {
    console.log('you are connected!');
});

window.addEventListener('DOMContentLoaded', () => {
    var messages = document.querySelector('#messages');
    var form = document.querySelector('#form');
    var input = document.querySelector('#input');

    form.addEventListener('submit', function (e) {
        e.preventDefault();
        if (input.value) {
            socket.emit('chat message', input.value);
            input.value = '';
        }
    });
})

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda