Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple ping pong socket.io example failing after a short time (data payload undefined) #3589

Closed
1 of 2 tasks
jt0dd opened this issue Apr 29, 2020 · 1 comment
Closed
1 of 2 tasks

Comments

@jt0dd
Copy link

jt0dd commented Apr 29, 2020

You want to:

  • report a bug
  • request a feature

Current behaviour

What is actually happening?

simple ping pong socket.io example failing after a short time (data payload undefined)

Steps to reproduce

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 (server and client both node.js 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)

Expected behaviour

I would expect this simple example over localhost to not break down. 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?

Setup

  • v12.16.1:
  • Windows:
  • runtime:
  • [email protected] & socket.io-client:
    (No browser involved)

Notes

I tried:

  • reinstalling socket.io
  • downgrading to 2.2.0
  • changing the ports to 3000 instead of 8080
  • restarting my PC
@jt0dd
Copy link
Author

jt0dd commented Apr 29, 2020

ping and pong are reserved events, hence the behavior. til

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant