This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
67 lines (57 loc) · 1.78 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as Socket from '@superviz/socket-client';
import { Subject } from 'rxjs';
import { Participant } from '../../common/types/participant.types';
import config from '../config/index';
export class IOC {
public state: Socket.ConnectionState;
public client: Socket.Realtime;
private stateSubject: Subject<Socket.ConnectionState> = new Subject();
constructor(participant: Participant) {
let environment = config.get<string>('environment') as 'dev' | 'prod';
environment = ['dev', 'prod'].includes(environment) ? environment : 'dev';
this.client = new Socket.Realtime(config.get<string>('apiKey'), environment, {
id: participant.id,
name: participant.name,
});
this.subscribeToDefaultEvents();
}
/**
* @function destroy
* @description Destroys the socket connection
* @returns {void}
*/
public destroy(): void {
this.client.destroy();
this.client.connection.off();
}
/**
* @function onStateChange
* @description Subscribe to the socket connection state changes
* @param next {Function}
* @returns {void}
*/
public onStateChange(next: (state: Socket.ConnectionState) => void): void {
this.stateSubject.subscribe(next);
}
/**
* @function subscribeToDefaultEvents
* @description subscribe to the default socket events
* @returns {void}
*/
private subscribeToDefaultEvents(): void {
this.client.connection.on((state) => {
this.state = state;
this.stateSubject.next(state);
});
}
/**
* @function createRoom
* @description create and join realtime room
* @param roomName {string}
* @returns {Room}
*/
public createRoom(roomName: string): Socket.Room {
const roomId = config.get<string>('roomId');
return this.client.connect(`${roomId}:${roomName}`);
}
}