fix: handle CORS in socket.io

This commit is contained in:
canove
2021-01-12 10:51:52 -03:00
parent 09b0c8e1f7
commit 2f7aeb738e

View File

@@ -1,4 +1,4 @@
import socketIo, { Server as SocketIO } from "socket.io"; import { Server as SocketIO } from "socket.io";
import { Server } from "http"; import { Server } from "http";
import AppError from "../errors/AppError"; import AppError from "../errors/AppError";
import { logger } from "../utils/logger"; import { logger } from "../utils/logger";
@@ -6,11 +6,15 @@ import { logger } from "../utils/logger";
let io: SocketIO; let io: SocketIO;
export const initIO = (httpServer: Server): SocketIO => { export const initIO = (httpServer: Server): SocketIO => {
io = socketIo(httpServer); io = new SocketIO(httpServer, {
cors: {
origin: process.env.FRONTEND_URL
}
});
io.on("connection", socket => { io.on("connection", socket => {
logger.info("Client Connected"); logger.info("Client Connected");
socket.on("joinChatBox", ticketId => { socket.on("joinChatBox", (ticketId: string) => {
logger.info("A client joined a ticket channel"); logger.info("A client joined a ticket channel");
socket.join(ticketId); socket.join(ticketId);
}); });
@@ -20,7 +24,7 @@ export const initIO = (httpServer: Server): SocketIO => {
socket.join("notification"); socket.join("notification");
}); });
socket.on("joinTickets", status => { socket.on("joinTickets", (status: string) => {
logger.info(`A client joined to ${status} tickets channel.`); logger.info(`A client joined to ${status} tickets channel.`);
socket.join(status); socket.join(status);
}); });
@@ -31,6 +35,7 @@ export const initIO = (httpServer: Server): SocketIO => {
}); });
return io; return io;
}; };
export const getIO = (): SocketIO => { export const getIO = (): SocketIO => {
if (!io) { if (!io) {
throw new AppError("Socket IO not initialized"); throw new AppError("Socket IO not initialized");