This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
Whisper through redis #583
Open
MrMadClown
wants to merge
6
commits into
tlaverdure:master
Choose a base branch
from
MrMadClown:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3b95b69
use Redis for client events, to enable multi-server whisper
MrMadClown bc13f46
KePrefix
MrMadClown e41b471
Create Publisher according to config
MrMadClown 47c9322
Fix options check
MrMadClown 149deed
Fix TypeError
MrMadClown 48f59fb
Whitespace
MrMadClown File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './redis-publisher'; | ||
export * from './publisher'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {Publisher} from "./publisher"; | ||
|
||
export class IoPublisher implements Publisher { | ||
constructor(private io) { | ||
} | ||
|
||
publish(channel: string, event: string, data: any): Promise<any> { | ||
if (event === "presence:leaving") { | ||
this.io | ||
.to(channel) | ||
.emit(event, data.data.member); | ||
|
||
return Promise.resolve(undefined); | ||
} | ||
|
||
if (event === "presence:joining") { | ||
this.io | ||
.sockets | ||
.connected[data.socket.id] | ||
.broadcast | ||
.to(channel) | ||
.emit(event, data.data.member); | ||
|
||
return Promise.resolve(undefined); | ||
} | ||
|
||
this.io.sockets | ||
.connected[data.socket.id] | ||
.broadcast | ||
.to(channel) | ||
.emit(event, channel, data.data); | ||
|
||
return Promise.resolve(undefined); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {Publisher} from "./publisher"; | ||
import {RedisPublisher} from "./redis-publisher"; | ||
import {IoPublisher} from "./io-publisher"; | ||
|
||
export class PublisherFactory { | ||
public constructor(private io) { } | ||
|
||
public create(options: any): Publisher { | ||
if (options.subscribers.redis) { | ||
return new RedisPublisher(options); | ||
} | ||
return new IoPublisher(this.io); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface Publisher { | ||
publish(channel: string, event: string, data: any): Promise<any>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
var Redis = require('ioredis'); | ||
import {Publisher} from './publisher'; | ||
|
||
var _ = require("lodash"); | ||
|
||
export class RedisPublisher implements Publisher { | ||
/** | ||
* Redis pub/sub client. | ||
* | ||
* @type {object} | ||
*/ | ||
private _redis: any; | ||
|
||
/** | ||
* | ||
* KeyPrefix for used in the redis Connection | ||
* | ||
* @type {String} | ||
*/ | ||
private readonly _keyPrefix: string; | ||
|
||
/** | ||
* Create a new instance of subscriber. | ||
* | ||
* @param {any} options | ||
*/ | ||
constructor(private options) { | ||
this._keyPrefix = options.databaseConfig.redis.keyPrefix || ''; | ||
this._redis = new Redis(options.databaseConfig.redis); | ||
} | ||
|
||
/** | ||
* Subscribe to events to broadcast. | ||
* | ||
* @return {Promise<any>} | ||
*/ | ||
publish(channel: string, event: string, data: any): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
this._redis.publish(this._keyPrefix + channel, JSON.stringify({event, ..._.omit(data, ['socket'])})); | ||
resolve(); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
le space