forked from janlukasschroeder/realtime-newsapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (32 loc) · 809 Bytes
/
index.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
#!/usr/bin/env node
const io = require('socket.io-client');
const config = require('./config');
const events = require('events');
const store = {};
const subscribe = () => {
store.socket.emit('action', {
type: 'subscribe',
filterId: config.io.filterId
});
};
const initSocket = () => {
const uri = config.io.server;
store.socket = io(uri);
store.socket.on('connect', subscribe);
store.socket.on('articles', handleNewArticles);
};
const handleNewArticles = ({ articles = [] }) => {
if (articles.length > 0) {
store.eventEmitter.emit('articles', articles);
}
};
module.exports.close = () => {
if (store.socket.close) {
store.socket.close();
}
};
module.exports = () => {
initSocket();
store.eventEmitter = new events.EventEmitter();
return store.eventEmitter;
};