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 = '';
}
});
})