migrated socket IO initialization to typescript

This commit is contained in:
canove
2020-09-20 10:45:49 -03:00
parent 04750df172
commit b7484afd63
5 changed files with 48 additions and 30 deletions

View File

@@ -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",

View File

@@ -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;
},
};

View File

@@ -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;
};

View File

@@ -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();

View File

@@ -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"