|
| 1 | +import pLimit from 'p-limit'; |
1 | 2 | import { IAdminForth, IWebSocketBroker, IWebSocketClient } from "../types/Back.js"; |
2 | 3 | import { AdminUser } from "../types/Common.js"; |
3 | 4 | import { afLogger } from '../modules/logger.js'; |
4 | 5 |
|
| 6 | +const PUBLISH_FILTER_CONCURRENCY = 10; |
| 7 | + |
5 | 8 | export default class SocketBroker implements IWebSocketBroker { |
6 | 9 | clients: IWebSocketClient[] = []; |
7 | 10 | topics: { [key: string]: IWebSocketClient[] } = {}; |
@@ -158,16 +161,27 @@ export default class SocketBroker implements IWebSocketBroker { |
158 | 161 | afLogger.trace(`No clients subscribed to topic ${topic}`); |
159 | 162 | return; |
160 | 163 | } |
161 | | - for (const client of this.topics[topic]) { |
162 | | - if (filterUsers) { |
| 164 | + const message = JSON.stringify({ type: 'message', topic, data }); |
| 165 | + |
| 166 | + if (!filterUsers) { |
| 167 | + for (const client of this.topics[topic]) { |
| 168 | + afLogger.trace(`Sending data to socket ${topic} ${JSON.stringify(data)}`); |
| 169 | + client.send(message); |
| 170 | + } |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + const limit = pLimit(PUBLISH_FILTER_CONCURRENCY); |
| 175 | + await Promise.all( |
| 176 | + this.topics[topic].map((client) => limit(async () => { |
163 | 177 | if (! (await filterUsers(client.adminUser)) ) { |
164 | 178 | afLogger.trace(`Client not authorized to receive message ${topic} ${client.adminUser}`); |
165 | | - continue; |
| 179 | + return; |
166 | 180 | } |
167 | | - } |
168 | | - afLogger.trace(`Sending data to socket ${topic} ${JSON.stringify(data)}`); |
169 | | - client.send(JSON.stringify({ type: 'message', topic, data })); |
170 | | - } |
| 181 | + afLogger.trace(`Sending data to socket ${topic} ${JSON.stringify(data)}`); |
| 182 | + client.send(message); |
| 183 | + })) |
| 184 | + ); |
171 | 185 | } |
172 | 186 |
|
173 | 187 | } |
0 commit comments