-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-client.js
110 lines (99 loc) · 3.49 KB
/
chat-client.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const WebSocket = require('ws');
const CONNECTING = 0;
const OPEN = 1;
const dev = true;
const PRODUCTION_CHAT_SERVER = 'wss://react-chat-server.herokuapp.com';
const DEV_CHAT_SERVER = 'ws://localhost:8080';
const chatServerUrl = dev ? DEV_CHAT_SERVER : PRODUCTION_CHAT_SERVER;
const createClient = token => {
let ws;
const listeners = {};
const handleMessage = m => {
const action = JSON.parse(m.data);
(listeners[action.type] || []).forEach(l => l(action));
};
return {
/**
* Inits the chat connection with the server
* @param {string} token the google auth session token
*/
init() {
if (ws) {
throw new Error('Chat client already initialized');
}
ws = new WebSocket(`${chatServerUrl}/${token}`);
ws.addEventListener('message', handleMessage);
},
/**
* Fetch the users list from server.
* @return {Promise} resolves to an array of users:
* {
* fullName: string,
* avatar: string,
* name: string,
* familyName: string,
* email: string,
* id: string
* }
*/
getUsers() {
this.send({type: 'getUsers', receiver: 'server'});
},
/**
* Sends a chat message to a recipient
* @param {string} messageText
* @param {string} receiver userId of the receiver. When not provided,
* the message is sent to all the users
*/
sendMessage(messageText, receiver = 'all') {
this.send({type: 'message', receiver, payload: {text: messageText}});
},
/**
* Sends an action to the chat server
* @param {object} action an action has the following form:
* {
* type: string, // for example 'message'
* receiver: string, // the userId of the recipient or 'all'
* [sender]: string, // not needed, the server knows you
* [time]: number, // automatically set to current timestamp.
* payload: object, // any data associated with the event
* }
* @return {[type]} [description]
*/
send(action) {
action.time = action.time || Date.now();
if (ws.readyState === CONNECTING) {
setTimeout(() => this.send(action), 100);
} else if (ws.readyState === OPEN) {
ws.send(JSON.stringify(action));
}
},
/**
* Registers a listener for a given event type
* @param {string} event
* @param {Function} listener will be called with every
* chat event of the specified type received from the sever
* @return {Function} dettach function to remove the event
* listener.
*/
on(event, listener) {
if (event in listeners) {
listeners[event].push(listener);
} else {
listeners[event] = [listener];
}
return () => this.off(event, listener);
},
/**
* Dettached a previously registered listener
* @param {string} event the event type
* @param {Function} listenerToRemove
*/
off(event, listenerToRemove) {
if (event in listeners) {
listeners[event] = listeners[event].filter(l => l !== listenerToRemove);
}
},
};
};
module.exports = createClient;