From 2f7aeb738e7f6bb349b66d4082d956b8bdb2bda9 Mon Sep 17 00:00:00 2001 From: canove Date: Tue, 12 Jan 2021 10:51:52 -0300 Subject: [PATCH] fix: handle CORS in socket.io --- backend/src/libs/socket.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/backend/src/libs/socket.ts b/backend/src/libs/socket.ts index d7eff50..7169e23 100644 --- a/backend/src/libs/socket.ts +++ b/backend/src/libs/socket.ts @@ -1,4 +1,4 @@ -import socketIo, { Server as SocketIO } from "socket.io"; +import { Server as SocketIO } from "socket.io"; import { Server } from "http"; import AppError from "../errors/AppError"; import { logger } from "../utils/logger"; @@ -6,11 +6,15 @@ import { logger } from "../utils/logger"; let io: SocketIO; export const initIO = (httpServer: Server): SocketIO => { - io = socketIo(httpServer); + io = new SocketIO(httpServer, { + cors: { + origin: process.env.FRONTEND_URL + } + }); io.on("connection", socket => { logger.info("Client Connected"); - socket.on("joinChatBox", ticketId => { + socket.on("joinChatBox", (ticketId: string) => { logger.info("A client joined a ticket channel"); socket.join(ticketId); }); @@ -20,7 +24,7 @@ export const initIO = (httpServer: Server): SocketIO => { socket.join("notification"); }); - socket.on("joinTickets", status => { + socket.on("joinTickets", (status: string) => { logger.info(`A client joined to ${status} tickets channel.`); socket.join(status); }); @@ -31,6 +35,7 @@ export const initIO = (httpServer: Server): SocketIO => { }); return io; }; + export const getIO = (): SocketIO => { if (!io) { throw new AppError("Socket IO not initialized");