budibase/packages/server/src/websocket.ts

27 lines
575 B
TypeScript
Raw Normal View History

2022-08-23 09:48:16 +02:00
import { Server } from "socket.io"
import http from "http"
2022-08-22 19:24:34 +02:00
class Websocket {
2022-08-23 09:48:16 +02:00
socketServer: Server
2022-08-22 19:24:34 +02:00
2022-08-23 09:48:16 +02:00
constructor(server: http.Server, path: string) {
this.socketServer = new Server(server, {
2022-08-22 19:24:34 +02:00
path,
})
}
// Emit an event to all sockets
emit(event: string, payload: any) {
2022-08-23 09:48:16 +02:00
this.socketServer.sockets.emit(event, payload)
2022-08-22 19:24:34 +02:00
}
}
// Likely to be more socket instances in future
let ClientAppSocket: Websocket
export const initialise = (server: http.Server) => {
ClientAppSocket = new Websocket(server, "/socket/client")
}
export { ClientAppSocket }