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
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
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'
ping
and pong
events are used in socket.io-client.
please see https://github.com/socketio/socket.io-client#events
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?
what a time waster. We all (including the socket.io team) had the same bad idea. So what ELSE is reserved?
Most helpful comment
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?