1

I have a very barebones socket.io example (between node.js client and server) I built because my overall socket.io connection kept reconnecting and this is an effort to debug what's going wrong:

socket-server.js

const app = require('express')();
const server = require('http').Server(app);
const io = require('socket.io')(server);
server.listen(8080);
console.log("listening...")
let counter = 0;
setInterval(() => {
  let data = { beat: counter };
  console.log("sending ping", data);
  io.sockets.emit('ping', data);
  counter++;
}, 1000);
io.on("connection", (socket) => {
  console.log("connected to client");
  socket.on("pong", (data) => {
    console.log("pong", data.beat);
  });
});

socket-client.js

var io = require('socket.io-client')
var socket = io.connect('http://127.0.0.1:8080/', {
  reconnect: true
});
socket.on('connect', function(socket) {
  console.log('connected to server');
});
socket.on('ping', function(data) {
  socket.emit('pong', data);
  console.log('ping', data.beat );
});

I start the server and then a few moments later start the client in two separate command prompts (Windows).

Output of socket-server.js:

listening...
sending ping { beat: 0 }
sending ping { beat: 1 }
sending ping { beat: 2 }
connected to client
sending ping { beat: 3 }
sending ping { beat: 4 }
sending ping { beat: 5 }
sending ping { beat: 6 }
sending ping { beat: 7 }
sending ping { beat: 8 }
sending ping { beat: 9 }
sending ping { beat: 10 }
sending ping { beat: 11 }
sending ping { beat: 12 }
sending ping { beat: 13 }
sending ping { beat: 14 }
sending ping { beat: 15 }
sending ping { beat: 16 }
sending ping { beat: 17 }
sending ping { beat: 18 }
sending ping { beat: 19 }
sending ping { beat: 20 }
sending ping { beat: 21 }
sending ping { beat: 22 }
sending ping { beat: 23 }
sending ping { beat: 24 }
sending ping { beat: 25 }
sending ping { beat: 26 }
sending ping { beat: 27 }
sending ping { beat: 28 }
sending ping { beat: 29 }
sending ping { beat: 30 }
sending ping { beat: 31 }
sending ping { beat: 32 }

Output of socket-client.js:

connected to server
ping 3
ping 4
ping 5
ping 6
ping 7
ping 8
ping 9
ping 10
ping 11
ping 12
ping 13
ping 14
ping 15
ping 16
ping 17
ping 18
ping 19
ping 20
ping 21
ping 22
ping 23
ping 24
ping 25
ping 26
ping 27
C:\Users\jonat\..proj-path..\tests\socket-client.js:10
  console.log('ping', data.beat );
                           ^

TypeError: Cannot read property 'beat' of undefined
    at Socket.<anonymous> (C:\Users\jonat\..proj-path..\tests\socket-client.js:10:28)

Since the connection over localhost, my network quality wouldn't be causing a breakdown of the connection, so what gives? How can I debug / fix this?

I tried:

  • reinstalling socket.io
  • downgrading to 2.2.0
  • changing the ports to 3000 instead of 8080
  • restarting my PC

1 Answer 1

3

Your code is not working because ping and pong are reserved events by the socket library: https://socket.io/docs/emit-cheatsheet/ (at the end).

In the output you showed the server side "pong" is also not working. I tried changing "pong" for "pang" and "ping" for "peng" and everything is working fine.

The most interesting part is that pong was just not working, but ping was working and receiving an undefined payload every 25 pings, so data was undefined and data.beat is an error. I would think is because socket IO is doing one of their own ping that include no payload every 25 seconds. But that's just guessing.

Anyhow, your code is fine, change the event name.

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.