0

I am trying to setup a socket in production like so:

client :

import openSocket from 'socket.io-client';

function handleSomething() {
    let socketServer = 'https://staging.app.xxx.eu:9999';
  
    const socket = openSocket(socketServer);

    socket.on('calendlyHook', () => {
        console.log("*** HERE");
        ...
        socket.emit('closeSocket');
        socket.close();
        socket.removeAllListeners();
    });
}

server :

const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);

function openSocket() {
    io.close();
    io.set('origins', '*:*');
    io.on('connection', (client) => {
        console.log('Socket connected');
        client.on('closeSocket', () => {
            io.close();
            io.removeAllListeners();
            console.log('Socket closed');
        });
    });

    const port = 9999;
    io.listen(port);
    console.log('*** listening on port ', port);
}

Then server-side, another function tries the following:

io.emit('calendlyHook');

or

io.sockets.emit('calendlyHook');

I have several issues in production (none of which happen on localhost):

  1. console.log('*** listening on port ', port) is working fine
  2. console.log('Socket connected') is not happening
  3. io.emit('calendlyHook') or io.sockets.emit('calendlyHook') are not doing anything

I do not have any web server proxy set up on that url.

What is wrong here? Thanks!

Wizzardzz
  • 781
  • 1
  • 9
  • 32
  • have you tried attaching an error listener on your sockets `socket.on('error', (e) => {...})` or `socket.on('connect_error', ...)`. As it works in dev it seems likely, that the error is not in the code but in the config of your production server. Do you have a valid certificate? Is there a firewall rule that allows traffic on port 9999? – derpirscher Sep 29 '20 at 12:15
  • As connection does not happen, and the problem only on production server, I think this an issue related to either `cors` or `ssl`, see this link https://stackoverflow.com/questions/24058157/socket-io-node-js-cross-origin-request-blocked – Abdulrahman Falyoun Sep 29 '20 at 12:16
  • @AbdulrahmanFalyoun thanks a lot, i will try that – Wizzardzz Sep 29 '20 at 12:45
  • @derpirscher logging the error is a great idea, `socket.on('error'...` would log client side right? Can I log server-side with something like `io.on('error`...`? – Wizzardzz Sep 29 '20 at 12:46
  • @Wizzardzz you should have a look into the docs ... – derpirscher Sep 29 '20 at 12:49
  • Where did you call the `handleSomething` function – Algo7 Sep 29 '20 at 14:20
  • @AvivLo from a button on the frontend, it also triggers `openSocket` – Wizzardzz Sep 29 '20 at 14:24
  • Will actually everything should go into ` io.on('connection', (client) => {}` Because that's where the connect is opened. Other events regarding socket io can only be triggered if and only if the connection event is fired. – Algo7 Sep 29 '20 at 14:42
  • I need `socket.on('calendlyHook', ()...` in the frontend because it does stuff on the page when it's called – Wizzardzz Sep 29 '20 at 14:44
  • 1
    Yes, and the back-end will only send you the response once the connection is initiated from the front end. When the connection is established, `io.on('connection', ......)` is fired. And under that, you then can do `socket.emit('calendyHook','msg_here')` which will send the msg to the front-end which `socket.on('calendlyHook`,...)` will handle. – Algo7 Sep 29 '20 at 21:30
  • @AvivLo this sounds like a great idea, I will try that. I still need to fix the issue with `io.on('connection'...` not firing up in production tho – Wizzardzz Sep 30 '20 at 08:05
  • So are all the issues solved except for `io.on('connection',....)` not working in production? Are you using apache2 as reverse proxy on your production server? If yes, may you show us you conf? – Algo7 Sep 30 '20 at 18:12
  • @AvivLo exactly, all solved except that one. The production server runs on Clever Cloud, no proxy that I know of. I even opened a port for the socket – Wizzardzz Oct 01 '20 at 08:11
  • Does the browser console says anything. If your are using chrome)firefox based browser, go to the network tab and have a look – Algo7 Oct 01 '20 at 14:04

0 Answers0