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

Shutting down adapter #13

Open
michaelzangl opened this issue May 18, 2023 · 2 comments
Open

Shutting down adapter #13

michaelzangl opened this issue May 18, 2023 · 2 comments

Comments

@michaelzangl
Copy link

I tried shutting down socket IO with the adapter installed. The node js process won't exit, since there are still timers active.

I expect this to work:

    socketIo.close();

There are still two timers running afterwards: the 30s cleanup timer and the ~2s reconnect timer

The workaround I found is:

    await socketIo.of('/').adapter.close(); // < this will clean the 30s cleanup timer
    socketIo.close();
    pool.connect = () => new Promise(() => {}); // < this will make the reconnect timer get blocked forever on the first reconnect attempt.
@grobarko
Copy link

The fix (socketio/socket.io@bf64870) still isn't working.

Currently I'm using this as a workaround:

await io.of('/').adapter.close();

io.close(() => {
  console.log("Server terminated gracefully 👌");
});
await pool.end();

@darrachequesne
Copy link
Member

@grobarko I was not able to reproduce the issue:

import { Server } from "socket.io";
import { createAdapter } from "@socket.io/postgres-adapter";
import pg from "pg";
import process from "node:process";

const PORT = process.env.PORT || 3000;

const pool = new pg.Pool({
  user: "postgres",
  host: "localhost",
  database: "postgres",
  password: "changeit",
  port: 5432,
});

await pool.query(`
  CREATE TABLE IF NOT EXISTS socket_io_attachments (
      id          bigserial UNIQUE,
      created_at  timestamptz DEFAULT NOW(),
      payload     bytea
  );
`);

pool.on("error", (err) => {
  console.error("Postgres error", err);
});

const io = new Server({
  adapter: createAdapter(pool)
});

io.on("connection", (socket) => {
  socket.on("hello", () => {
    // send to anyone except the sender
    socket.broadcast.emit("hello", socket.id, process.pid);
  });
});

io.listen(PORT);
console.log(`server listening on port ${PORT}`);

setTimeout(async () => {
  console.log("STOP !!!");

  io.close();
  await pool.end();
}, 1000);

See example here: https://github.com/socketio/socket.io/tree/main/examples/postgres-adapter-example

Calling io.close() seems sufficient (with pool.end(), but that makes sense since we don't own that object). Could you please check?

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

3 participants