import { Server } from 'http' ; import { Server as wsServer } from 'ws' ; export const useWebSockets = ( server ) : wsServer => { const wss = new wsServer ( { server } ) ; wss . on ( 'connection' , ( ws , req ) => { ws . on ( 'message' , message => { if ( message && typeof message === 'string' ) { try { ws . send ( JSON . stringify ( { ... JSON . parse ( message ) , server : 'Relayed from' } ) ) ; } catch ( err ) { ws . send ( 'an error occurred in the ws channel' ) ; } } } ) ; } ) ; return wss ; } ;