diff --git a/Readme.md b/Readme.md index 7834c8c..c4b4819 100644 --- a/Readme.md +++ b/Readme.md @@ -161,6 +161,7 @@ option set to `true`. The following options are allowed: - `key`: the name of the key to pub/sub events on as prefix (`socket.io`) +- `parser`: parser to use for encoding messages to Redis ([`notepack.io](https://www.npmjs.com/package/notepack.io)) ### Emitter#to(room:String):BroadcastOperator ### Emitter#in(room:String):BroadcastOperator diff --git a/lib/index.ts b/lib/index.ts index 545c8e0..8bd29e1 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -27,17 +27,27 @@ enum RequestType { SERVER_SIDE_EMIT = 6, } +interface Parser { + encode: (msg: any) => any; +} + export interface EmitterOptions { /** * @default "socket.io" */ key?: string; + /** + * The parser to use for encoding messages sent to Redis. + * Defaults to notepack.io, a MessagePack implementation. + */ + parser?: Parser; } interface BroadcastOptions { nsp: string; broadcastChannel: string; requestChannel: string; + parser: Parser; } interface BroadcastFlags { @@ -57,6 +67,7 @@ export class Emitter { this.opts = Object.assign( { key: "socket.io", + parser: msgpack, }, opts ); @@ -64,6 +75,7 @@ export class Emitter { nsp, broadcastChannel: this.opts.key + "#" + nsp + "#", requestChannel: this.opts.key + "-request#" + nsp + "#", + parser: this.opts.parser, }; } @@ -366,7 +378,7 @@ export class BroadcastOperator except: [...this.exceptRooms], }; - const msg = msgpack.encode([UID, packet, opts]); + const msg = this.broadcastOptions.parser.encode([UID, packet, opts]); let channel = this.broadcastOptions.broadcastChannel; if (this.rooms && this.rooms.size === 1) { channel += this.rooms.keys().next().value + "#";