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

Are "ping" and/or "pong" reserved events? #2414

Closed
thebinarypenguin opened this issue Jan 28, 2016 · 4 comments
Closed

Are "ping" and/or "pong" reserved events? #2414

thebinarypenguin opened this issue Jan 28, 2016 · 4 comments

Comments

@thebinarypenguin
Copy link

I have some basic code (see below) that works as expected when the events are named ding and dong but behaves very strangely when the events are named ping and pong

When the events are named ping and pong

  • The server code will log the "connect" and "disconnect" messages, but not the "ping" and "pong" messages
  • The client code will log the "ping" message and after a long wait (10+ seconds) will log the "pong" message, and if I keep waiting I'll get more "pong" message each 10+ seconds apart

Server Code

const fs             = require('fs');
const http           = require('http');
const path           = require('path');
const SocketIOServer = require('socket.io');

const app = new http.Server();
app.on('request', (req, res) => {

  const index = path.join(__dirname, 'public', 'index.html')

  fs.readFile(index, function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
});
app.listen(3000, 'localhost');

const io = new SocketIOServer(app);


// could also use "connection"
io.on('connect', function (socket) {
  console.log(`${socket.id} "connect"`);

  socket.on('ping', (data) => {
    console.log('Receive "ping"');

    io.emit('pong', {});  
    console.log('Send "pong"');  
  });

  socket.on('disconnect', () => {
    console.log(`${socket.id} "disconnect"`);    
  });
});

Client Code

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Socket.IO Events</title>
  </head>
  <body>
    <h1>Socket.IO Events</h1>

    <script src="/socket.io/socket.io.js"></script>
    <script>
      const socket = io();

      socket.on('pong', (data) => {
        console.log('Receive "pong"');
      });

      socket.emit('ping', {});
      console.log('Send "ping"');

    </script>
  </body>
</html>

I have no idea what is going on here. Also this may be related to #1951

@neutrino84
Copy link

I'm seeing strange behavior while using 'ping' / 'pong' event names - there does appear to be code in socket.io that does something with ping and pong, I don't have time to investigate further - changed name to 'drip' and 'drop'

@nkzawa
Copy link
Contributor

nkzawa commented Jan 29, 2016

ping and pong events are used in socket.io-client.
please see https://github.com/socketio/socket.io-client#events

@nkzawa nkzawa closed this as completed Jan 29, 2016
@ajhsu
Copy link

ajhsu commented Apr 7, 2016

Just trapped by the 'ping' event for few hours.
I think the official document may sync up with the document on GitHub?
Or maybe reserved event names can be noted on the document so that people won't be trapped again?

@basilbeltran
Copy link

basilbeltran commented Aug 23, 2016

what a time waster. We all (including the socket.io team) had the same bad idea. So what ELSE is reserved?

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

5 participants