autoloop / apps /server /src /lib /socket.ts
shubhjn's picture
Deploy AutoLoop Server to Hugging Face
a6b6c66
raw
history blame contribute delete
993 Bytes
import { Server as SocketServer } from 'socket.io';
import { Server as HttpServer } from 'http';
let io: SocketServer | null = null;
export function initSocket(server: any) {
io = new SocketServer(server, {
cors: {
origin: "*", // Adjust in production
methods: ["GET", "POST"]
},
transports: ['websocket', 'polling']
});
io.on('connection', (socket) => {
console.log(`[Socket] Client connected: ${socket.id}`);
socket.on('subscribe', (userId: string) => {
console.log(`[Socket] User ${userId} subscribed to updates`);
socket.join(`user:${userId}`);
});
socket.on('disconnect', () => {
console.log(`[Socket] Client disconnected: ${socket.id}`);
});
});
return io;
}
export function getIO() {
if (!io) {
console.warn("[Socket] IO not initialized!");
}
return io;
}
export function emitToUser(userId: string, event: string, data: any) {
if (io) {
io.to(`user:${userId}`).emit(event, data);
}
}