chore: Allow websockets and collaboration service to run in the same process (#2674)

This commit is contained in:
Tom Moor
2021-10-19 21:18:20 -07:00
committed by GitHub
parent 3610a7f4a2
commit d443abfc57
11 changed files with 67 additions and 76 deletions

View File

@ -16,13 +16,28 @@ import { getUserForJWT } from "../utils/jwt";
const { can } = policy;
export default function init(app: Koa, server: http.Server) {
const path = "/realtime";
// Websockets for events and non-collaborative documents
const io = IO(server, {
path: "/realtime",
path,
serveClient: false,
cookie: false,
});
// Remove the upgrade handler that we just added when registering the IO engine
// And re-add it with a check to only handle the realtime path, this allows
// collaboration websockets to exist in the same process as engine.io.
const listeners = server.listeners("upgrade");
const ioHandleUpgrade = listeners.pop();
server.removeListener("upgrade", ioHandleUpgrade);
server.on("upgrade", function (req, socket, head) {
if (req.url.indexOf(path) > -1) {
ioHandleUpgrade(req, socket, head);
}
});
server.on("shutdown", () => {
Metrics.gaugePerInstance("websockets.count", 0);
});