diff --git a/backend/package.json b/backend/package.json index 6719105..4cecba0 100644 --- a/backend/package.json +++ b/backend/package.json @@ -38,6 +38,7 @@ "@types/jsonwebtoken": "^8.5.0", "@types/multer": "^1.4.4", "@types/node": "^14.10.1", + "@types/socket.io": "^2.1.11", "@types/validator": "^13.1.0", "@types/yup": "^0.29.7", "@typescript-eslint/eslint-plugin": "^4.1.0", diff --git a/backend/src/libs/socket.js b/backend/src/libs/socket.js deleted file mode 100644 index 5e3b8c7..0000000 --- a/backend/src/libs/socket.js +++ /dev/null @@ -1,14 +0,0 @@ -let io; - -module.exports = { - init: httpServer => { - io = require("socket.io")(httpServer); - return io; - }, - getIO: () => { - if (!io) { - throw new Error("Socket IO not initialized"); - } - return io; - }, -}; diff --git a/backend/src/libs/socket.ts b/backend/src/libs/socket.ts new file mode 100644 index 0000000..ec82552 --- /dev/null +++ b/backend/src/libs/socket.ts @@ -0,0 +1,15 @@ +import socketIo, { Server as SocketIO } from "socket.io"; +import { Server } from "http"; + +let io: SocketIO; + +export const initIO = (httpServer: Server): SocketIO => { + io = socketIo(httpServer); + return io; +}; +export const getIO = (): SocketIO => { + if (!io) { + throw new Error("Socket IO not initialized"); + } + return io; +}; diff --git a/backend/src/server.ts b/backend/src/server.ts index 21fbd79..93519f2 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -4,11 +4,12 @@ import "express-async-errors"; import express, { Request, Response, NextFunction } from "express"; import cors from "cors"; import multer from "multer"; -import Sentry from "@sentry/node"; +import * as Sentry from "@sentry/node"; import uploadConfig from "./config/upload"; import AppError from "./errors/AppError"; import routes from "./routes"; +import { initIO } from "./libs/socket"; import "./database"; // import path from "path"; @@ -43,23 +44,23 @@ const server = app.listen(process.env.PORT, () => { console.log(`Server started on port: ${process.env.PORT}`); }); -// const io = require("./libs/socket").init(server); -// io.on("connection", socket => { -// console.log("Client Connected"); -// socket.on("joinChatBox", ticketId => { -// console.log("A client joined a ticket channel"); -// socket.join(ticketId); -// }); +const io = initIO(server); +io.on("connection", socket => { + console.log("Client Connected"); + socket.on("joinChatBox", ticketId => { + console.log("A client joined a ticket channel"); + socket.join(ticketId); + }); -// socket.on("joinNotification", () => { -// console.log("A client joined notification channel"); -// socket.join("notification"); -// }); + socket.on("joinNotification", () => { + console.log("A client joined notification channel"); + socket.join("notification"); + }); -// socket.on("disconnect", () => { -// console.log("Client disconnected"); -// }); -// }); + socket.on("disconnect", () => { + console.log("Client disconnected"); + }); +}); // const startWhatsAppSessions = async () => { // const whatsapps = await Whatsapp.findAll(); diff --git a/backend/yarn.lock b/backend/yarn.lock index b3bac1c..012bfb5 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -189,6 +189,13 @@ dependencies: "@types/express" "*" +"@types/engine.io@*": + version "3.1.4" + resolved "https://registry.yarnpkg.com/@types/engine.io/-/engine.io-3.1.4.tgz#3d9472711d179daa7c95c051e50ad411e18a9bdc" + integrity sha512-98rXVukLD6/ozrQ2O80NAlWDGA4INg+tqsEReWJldqyi2fulC9V7Use/n28SWgROXKm6003ycWV4gZHoF8GA6w== + dependencies: + "@types/node" "*" + "@types/express-serve-static-core@*": version "4.17.12" resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.12.tgz#9a487da757425e4f267e7d1c5720226af7f89591" @@ -265,6 +272,14 @@ "@types/express-serve-static-core" "*" "@types/mime" "*" +"@types/socket.io@^2.1.11": + version "2.1.11" + resolved "https://registry.yarnpkg.com/@types/socket.io/-/socket.io-2.1.11.tgz#e0d6759880e5f9818d5297a3328b36641bae996b" + integrity sha512-bVprmqPhJMLb9ZCm8g0Xy8kwBFRbnanOWSxzWkDkkIwxTvud5tKMfAJymXX6LQbizUKCS1yima7JM4BeLqjNqA== + dependencies: + "@types/engine.io" "*" + "@types/node" "*" + "@types/strip-bom@^3.0.0": version "3.0.0" resolved "https://registry.yarnpkg.com/@types/strip-bom/-/strip-bom-3.0.0.tgz#14a8ec3956c2e81edb7520790aecf21c290aebd2"