Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit f4157d2

Browse files
authored
Merge pull request #333 from oxan/configurable-subscribers
Support enabling and disabling individual subscribers
2 parents 96e8a93 + a8e4c70 commit f4157d2

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/echo-server.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpSubscriber, RedisSubscriber } from './subscribers';
1+
import { HttpSubscriber, RedisSubscriber, Subscriber } from './subscribers';
22
import { Channel } from './channels';
33
import { Server } from './server';
44
import { HttpApi } from './api';
@@ -35,6 +35,10 @@ export class EchoServer {
3535
sslKeyPath: '',
3636
sslCertChainPath: '',
3737
sslPassphrase: '',
38+
subscribers: {
39+
http: true,
40+
redis: true
41+
},
3842
apiOriginAllow: {
3943
allowCors: false,
4044
allowOrigin: '',
@@ -65,18 +69,11 @@ export class EchoServer {
6569
private channel: Channel;
6670

6771
/**
68-
* Redis subscriber instance.
69-
*
70-
* @type {RedisSubscriber}
71-
*/
72-
private redisSub: RedisSubscriber;
73-
74-
/**
75-
* Http subscriber instance.
72+
* Subscribers
7673
*
77-
* @type {HttpSubscriber}
74+
* @type {Subscriber[]}
7875
*/
79-
private httpSub: HttpSubscriber;
76+
private subscribers: Subscriber[];
8077

8178
/**
8279
* Http api instance.
@@ -119,8 +116,13 @@ export class EchoServer {
119116
init(io: any): Promise<any> {
120117
return new Promise((resolve, reject) => {
121118
this.channel = new Channel(io, this.options);
122-
this.redisSub = new RedisSubscriber(this.options);
123-
this.httpSub = new HttpSubscriber(this.server.express, this.options);
119+
120+
this.subscribers = [];
121+
if (this.options.subscribers.http)
122+
this.subscribers.push(new HttpSubscriber(this.server.express, this.options));
123+
if (this.options.subscribers.redis)
124+
this.subscribers.push(new RedisSubscriber(this.options));
125+
124126
this.httpApi = new HttpApi(io, this.channel, this.server.express, this.options.apiOriginAllow);
125127
this.httpApi.init();
126128

@@ -152,15 +154,13 @@ export class EchoServer {
152154
*/
153155
listen(): Promise<any> {
154156
return new Promise((resolve, reject) => {
155-
let http = this.httpSub.subscribe((channel, message) => {
156-
return this.broadcast(channel, message);
157-
});
158-
159-
let redis = this.redisSub.subscribe((channel, message) => {
160-
return this.broadcast(channel, message);
157+
let subscribePromises = this.subscribers.map(subscriber => {
158+
return subscriber.subscribe((channel, message) => {
159+
return this.broadcast(channel, message);
160+
});
161161
});
162162

163-
Promise.all([http, redis]).then(() => resolve());
163+
Promise.all(subscribePromises).then(() => resolve());
164164
});
165165
}
166166

0 commit comments

Comments
 (0)