Skip to content

Commit

Permalink
using express middleware for socket
Browse files Browse the repository at this point in the history
  • Loading branch information
huytran17 committed May 25, 2024
1 parent d90b466 commit c1f8957
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions core/server/src/config/socket.io/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Server as HTTPServer } from "http";
import { Http2SecureServer, Http2Server } from "http2";
import type { Server as HTTPSServer } from "https";
import { Server } from "socket.io";
import { Compression, CookieParser, Helmet } from "../../app";
import { initialAdminNsp, initialClientNsp } from "./nsp";

export type TServerInstance =
Expand All @@ -14,7 +15,17 @@ export default class SocketIO {
public static socket_instance: SocketIO;
io_client: Server;

constructor(http_srv: TServerInstance) {
constructor({
http_srv,
helmet,
compression,
cookieParser,
}: {
http_srv: TServerInstance;
helmet: Helmet;
compression: Compression;
cookieParser: CookieParser;
}) {
if (SocketIO.socket_instance) {
return SocketIO.socket_instance;
}
Expand All @@ -25,12 +36,17 @@ export default class SocketIO {
process.env.USER_DASHBOARD_URL,
process.env.ADMIN_DASHBOARD_URL,
],
credentials: true,
methods: "*",
},
});

initialAdminNsp(this.io_client);
initialClientNsp(this.io_client);
this.io_client.engine.use(compression());
this.io_client.engine.use(helmet());
this.io_client.engine.use(cookieParser());

initialAdminNsp({ io: this.io_client });
initialClientNsp({ io: this.io_client });

SocketIO.socket_instance = this;

Expand Down

0 comments on commit c1f8957

Please sign in to comment.