한쪽에서 이벤트를 발행하고, 다른쪽에서는 해당 이벤트 리스너를 등록하는 방법
예시1. 서버에서 이벤트를 전송하고, 클라이언트에서 해당 이벤트 리스너를 등록
// server-side
io.on("connection", (socket) => {
  socket.emit("hello", "world");
});
// client-side
socket.on("hello", (arg) => {
  console.log(arg); // world
});
// client-side socket.emit(“hello”, “world”);
- **예시3. 이벤트와 함께 데이터를 전송하는 방법**
```javascript
// server-side
io.on("connection", (socket) => {
  socket.emit("hello", 1, "2", { 3: '4', 5: Buffer.from([6]) });
});
// client-side
socket.on("hello", (arg1, arg2, arg3) => {
  console.log(arg1); // 1
  console.log(arg2); // "2"
  console.log(arg3); // { 3: '4', 5: ArrayBuffer (1) [ 6 ] }
});
const serializedMap = [...myMap.entries()];
const serializedSet = [...mySet.keys()];
// server-side
io.on("connection", (socket) => {
  socket.on("update item", (arg1, arg2, callback) => {
    console.log(arg1); // 1
    console.log(arg2); // { name: "updated" }
    callback({
      status: "ok"
    });
  });
});
// client-side
socket.emit("update item", "1", { name: "updated" }, (response) => {
  console.log(response.status); // ok
});
const withTimeout = (onSuccess, onTimeout, timeout) => {
  let called = false;
  const timer = setTimeout(() => {
    if (called) return;
    called = true;
    onTimeout();
  }, timeout);
  return (...args) => {
    if (called) return;
    called = true;
    clearTimeout(timer);
    onSuccess.apply(this, args);
  }
}
socket.emit("hello", 1, 2, withTimeout(() => {
  console.log("success!");
}, () => {
  console.log("timeout!");
}, 1000));
// server-side
io.on("connection", (socket) => {
  console.log("connect");
  socket.on("ping", (count) => {
    console.log(count);
  });
});
// client-side
let count = 0;
setInterval(() => {
  socket.volatile.emit("ping", ++count);
}, 1000);
connect
1
2
3
4
# the server is restarted, the client automatically reconnects
connect
9
10
11
1
2
3
4
# the server is restarted, the client automatically reconnects and sends its buffered events
connect
5
6
7
8
9
10
11