feat: add socket server for notification
This commit is contained in:
parent
aca6db3dce
commit
4db3ab4c84
2 changed files with 72 additions and 0 deletions
|
|
@ -6,10 +6,13 @@ import swaggerUi from "swagger-ui-express";
|
||||||
import swaggerDocument from "./swagger.json";
|
import swaggerDocument from "./swagger.json";
|
||||||
import error from "./middlewares/error";
|
import error from "./middlewares/error";
|
||||||
import { RegisterRoutes } from "./routes";
|
import { RegisterRoutes } from "./routes";
|
||||||
|
import { initWebSocket } from "./services/socket";
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
initWebSocket(+(process.env.SOCKET_PORT || 3001));
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(express.urlencoded({ extended: true }));
|
app.use(express.urlencoded({ extended: true }));
|
||||||
|
|
|
||||||
69
src/services/socket.ts
Normal file
69
src/services/socket.ts
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
import { Server } from "socket.io";
|
||||||
|
|
||||||
|
let io: Server;
|
||||||
|
|
||||||
|
export function initWebSocket(port?: number) {
|
||||||
|
if (io) return;
|
||||||
|
|
||||||
|
io = new Server({ cors: { origin: "*" }, path: "/api/v1/backup-socket" });
|
||||||
|
|
||||||
|
io.use(async (socket, next) => {
|
||||||
|
const token = socket.handshake.auth.token;
|
||||||
|
|
||||||
|
const res = await fetch(`${process.env.AUTH_REALM_URL}/protocol/openid-connect/userinfo`, {
|
||||||
|
headers: { authorization: `Bearer ${token}` },
|
||||||
|
}).catch((e) => console.error(e));
|
||||||
|
|
||||||
|
if (res?.ok) {
|
||||||
|
socket.data.user = await res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
io.on("connection", (ws) => {
|
||||||
|
console.log("✅ Client connected to WebSocket");
|
||||||
|
|
||||||
|
ws.on("close", () => {
|
||||||
|
console.log("❌ Client disconnected");
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on("error", (error: any) => {
|
||||||
|
console.error("WebSocket error:", error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
io.listen(port || 3001);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function sendWebSocket(
|
||||||
|
event: string,
|
||||||
|
data: any,
|
||||||
|
opts?: {
|
||||||
|
roles?: string[];
|
||||||
|
userId?: string[];
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
if (!io) initWebSocket();
|
||||||
|
|
||||||
|
for (let [id, session] of io.of("/").sockets) {
|
||||||
|
const user: {
|
||||||
|
sub: string;
|
||||||
|
name: string;
|
||||||
|
given_name: string;
|
||||||
|
family_name: string;
|
||||||
|
preferred_username: string;
|
||||||
|
email: string;
|
||||||
|
role: string[];
|
||||||
|
} = session.data.user;
|
||||||
|
|
||||||
|
if (!user) continue;
|
||||||
|
|
||||||
|
if (
|
||||||
|
user.role?.some((v) => opts?.roles?.includes(v)) ||
|
||||||
|
opts?.userId?.some((v) => user.sub === v)
|
||||||
|
) {
|
||||||
|
io.to(id).emit(event, JSON.stringify(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue