I'm in the process of building a chat application with the t3 stack. I've successfully created a socket endpoint using JavaScript, but now I'm facing some challenges as I try to convert it to TypeScript.
import { Server } from "Socket.IO";
const SocketHandler = (req, res) => {
if (res.socket.server.io) {
console.log("Socket is already running");
} else {
console.log("Socket is initializing");
const io = new Server(res.socket.server);
res.socket.server.io = io;
io.on("connection", (socket) => {
socket.on("update-messages", (msg) => {
socket.broadcast.emit("update-messages", msg);
});
socket.on("user-typing", (msg) => {
socket.broadcast.emit("user-typing", msg);
});
});
}
res.end();
};
export default SocketHandler;
I attempted translating it into TypeScript by using "any" as the type for the response object, but I'm not satisfied with this approach.