-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2c848b
commit 0607d99
Showing
6 changed files
with
91 additions
and
10 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,59 @@ | ||
export class Rediscast {} | ||
import { Redis, type RedisOptions } from "ioredis" | ||
import type { WsxSocket } from "../socket" | ||
import { Broadcast, broadcastSymbols } from "./broadcast" | ||
import type { Topic } from "./topic" | ||
|
||
/** | ||
* Redis broadcast. Shares actions via Redis Pub/Sub | ||
*/ | ||
export class Rediscast extends Broadcast { | ||
redis: Redis | ||
prefix: string | ||
|
||
constructor({ | ||
redis, | ||
prefix = "wsx__", | ||
}: { redis: RedisOptions; prefix?: string }) { | ||
super() | ||
this.redis = new Redis(redis) | ||
this.prefix = prefix | ||
|
||
this.redis.on("message", (channel, message) => { | ||
const key = this.unredisKey(channel) | ||
const topic = this.topics.get(key) | ||
if (topic) { | ||
this[broadcastSymbols.publish](topic, JSON.parse(message)) | ||
} | ||
}) | ||
} | ||
|
||
private redisKey(key: string) { | ||
return `${this.prefix}${key}` | ||
} | ||
|
||
private unredisKey(redisKey: string) { | ||
return redisKey.slice(this.prefix.length) | ||
} | ||
|
||
async create(key: string) { | ||
const redisKey = this.redisKey(key) | ||
const topic = super.create(redisKey) | ||
await this.redis.ssubscribe(redisKey) | ||
return topic | ||
} | ||
|
||
remove(topic: string) { | ||
super.remove(topic) | ||
this.redis.sunsubscribe(this.redisKey(topic)) | ||
} | ||
|
||
async [broadcastSymbols.publish]( | ||
topic: Topic, | ||
data: unknown, | ||
except?: WsxSocket, | ||
) { | ||
super[broadcastSymbols.publish](topic, data, except) | ||
const redisKey = this.redisKey(topic.key) | ||
await this.redis.spublish(redisKey, JSON.stringify(data)) | ||
} | ||
} |
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 @@ | ||
export const publish = Symbol("publish") |
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