-
I want to pass my typescript types outside of the This is how my setup looks like: // types for socket.io
export interface ServerToClientEvents {
RCV_MSG: (data: {
fromUserId: number;
type: string;
message: string;
toTeam: boolean;
}) => void;
get_room_after_connect: (room: Room) => void;
} // index.ts where I initialize my socket.io server
const io = new Server<ServerToClientEvents>(server, {
cors: {
origin: [
process.env.CORS_ORIGIN,
process.env.SOCKET_ADMIN_DASHBOARD_ORIGIN,
],
credentials: true,
},
});
io.on("connection", async (socket) => {
// here I'm able to access the type for get_room_after_connect from ServerToClientEvents interface
socket.emit("get_room_after_connect", room!);
// this is a custom function I created where I have various functions which will emit something using socket.io to client once the logic finishes
// I'm passing the socket object to handleUserEvents which will then distribute it to the various functions
// the problem I'm facing is that I'm not able to access my types defined in ServerToClientEvents interface in handleUserEvents when I emit something to the client from there
handleUserEvents({ socket, io, redis, currentUserId });
});
Please let me know if I'm doing something wrong or Socket.io does not support this functionality. Note: please do read my comments in code to understand the code snippets better Thanks |
Beta Was this translation helpful? Give feedback.
Answered by
Rec0iL99
Sep 6, 2022
Replies: 1 comment
-
refer #4455 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Rec0iL99
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
refer #4455