Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Commit

Permalink
revert one subscriber per topic and ensure only open sockets get topics
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrouid committed May 9, 2019
1 parent 72013ce commit 2404aba
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/pubsub.ts
Expand Up @@ -2,14 +2,10 @@ import WebSocket from 'ws'
import { ISocketMessage, ISocketSub } from './types'
import { pushNotification } from './notification'

let subs: ISocketSub[] = []
const subs: ISocketSub[] = []
const pubs: ISocketMessage[] = []

const setSub = (subscriber: ISocketSub) => {
const _subs = subs.filter(sub => sub.topic !== subscriber.topic)
_subs.push(subscriber)
subs = _subs
}
const setSub = (subscriber: ISocketSub) => subs.push(subscriber)
const getSub = (topic: string) =>
subs.filter(subscriber => subscriber.topic === topic)

Expand Down Expand Up @@ -49,9 +45,11 @@ const PubController = (socketMessage: ISocketMessage) => {
pushNotification(socketMessage.topic)

if (subscribers.length) {
subscribers.forEach((subscriber: ISocketSub) =>
socketSend(subscriber.socket, socketMessage)
)
subscribers
.filter((subscriber: ISocketSub) => subscriber.socket.readyState === 1)
.forEach((subscriber: ISocketSub) =>
socketSend(subscriber.socket, socketMessage)
)
} else {
setPub(socketMessage)
}
Expand Down

0 comments on commit 2404aba

Please sign in to comment.