Merge pull request #43 from inf3cti0n95/rxjs-6

feat: Updated the current code to RxJS 6.
master
Viraj Trivedi 6 years ago committed by GitHub
commit 7011467eb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,10 +5,10 @@
[![npm](https://img.shields.io/npm/v/rocket.chat.realtime.api.rxjs.svg)](https://www.npmjs.com/package/rocket.chat.realtime.api.rxjs)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
Abstraction for Utilizing [Rocket.Chat](https://rocket.chat/)'s [Realtime API](https://rocket.chat/docs/developer-guides/realtime-api) Methods with [RxJS](http://reactivex.io/rxjs/)
## Installation
```
npm install --save rocket.chat.realtime.api.rxjs
```
@ -22,7 +22,7 @@ import { RealTimeAPI } from "rocket.chat.realtime.api.rxjs";
const realTimeAPI = new RealTimeAPI("wss://demo.rocket.chat/websocket");
// Provide, URL to the Rocket.Chat's Realtime API.
realTimeAPI.keepAlive();
realTimeAPI.keepAlive().subscribe();
// Responds "pong" to the "ping" message sent by the Realtime API. To keep the connection alive.
const auth = realTimeApi.login(USERNAME, PASSWORD);
@ -43,22 +43,21 @@ const auth = realTimeApi.login(USERNAME, PASSWORD);
## Methods
| Methods | Functionality |
|------------------------------------------------------------ |----------------------------------------------------------------------------------------------- |
| connectToServer() | Initiates Connections to the Server to the RealTime API. Returns Observable with the server's response |
| keepAlive() | Responds "pong" to the "ping" message sent by the Realtime API. To keep the connection alive. |
| login(username, password) | Returns Observable to the Result/Response from the RealTime API. |
| loginWithAuthToken(authToken) | Returns Observable to the Result/Response from the RealTime API. |
| loginWithOAuth(credToken, credSecret) | Returns Observable to the Result/Response from the RealTime API. |
| callMethod(methodName, ...params) | Returns Observable to the Result of Method Call from Rocket.Chat Realtime API |
| sendMessage(jsonObject) | Sends the JSON Object to the API Server |
| onMessage( message => console.log(message) ) | Subscribes to the Messages sent from the server |
| onError( error => console.error(error) ) | Subscribes to the Errors. |
| onCompletion(() => console.info("Complete")) | Subscribes to Completion on the Websocket Connection |
| subscribe(messageHandler, errorHandler, completionHandler) | Subscribes to All three i.e - messages, errors and completion |
| getObservable() | Returns observable of the WebSocket Connection to the RealTime API |
| disconnect() | Disconnect the WebSocket Connection between client and RealTime API |
| | |
| Methods | Functionality |
| ---------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| connectToServer() | Initiates Connections to the Server to the RealTime API. Returns Observable with the server's response |
| keepAlive() | Returns Observable to subscribe which Responds "pong" to the "ping" message sent by the Realtime API. To keep the connection alive. |
| login(username, password) | Returns Observable to the Result/Response from the RealTime API. |
| loginWithAuthToken(authToken) | Returns Observable to the Result/Response from the RealTime API. |
| loginWithOAuth(credToken, credSecret) | Returns Observable to the Result/Response from the RealTime API. |
| callMethod(methodName, ...params) | Returns Observable to the Result of Method Call from Rocket.Chat Realtime API |
| sendMessage(jsonObject) | Sends the JSON Object to the API Server |
| onMessage( message => console.log(message) ) | Subscribes to the Messages sent from the server |
| onError( error => console.error(error) ) | Subscribes to the Errors. |
| onCompletion(() => console.info("Complete")) | Subscribes to Completion on the Websocket Connection |
| subscribe(messageHandler, errorHandler, completionHandler) | Subscribes to All three i.e - messages, errors and completion |
| getObservable() | Returns observable of the WebSocket Connection to the RealTime API |
| disconnect() | Disconnect the WebSocket Connection between client and RealTime API |
| | |
### Checkout the Rocket.Chat's [RealTime API documentation](https://rocket.chat/docs/developer-guides/realtime-api) for further information on working of the RealTime API.

@ -1,16 +1,14 @@
/**
* Rocket.Chat RealTime API
*/
import { Observable } from "rxjs";
import { WebSocketSubject } from 'rxjs/observable/dom/WebSocketSubject';
import { WebSocketSubject } from "rxjs/webSocket";
export declare class RealTimeAPI {
url: string;
webSocket: WebSocketSubject<{}>;
constructor(param: string | WebSocketSubject<{}>);
webSocket: WebSocketSubject<any>;
constructor(param: string | WebSocketSubject<any>);
/**
* Returns the Observable to the RealTime API Socket
*/
getObservable(): Observable<any>;
getObservable(): WebSocketSubject<any>;
/**
* Disconnect the WebSocket Connection between client and RealTime API
*/
@ -34,45 +32,45 @@ export declare class RealTimeAPI {
/**
* sendMessage to Rocket.Chat Server
*/
sendMessage(messageObject: {}): void;
sendMessage(messageObject: any): void;
/**
* getObservableFilteredByMessageType
*/
getObservableFilteredByMessageType(messageType: string): Observable<any>;
getObservableFilteredByMessageType(messageType: string): import("rxjs/internal/Observable").Observable<any>;
/**
* getObservableFilteredByID
*/
getObservableFilteredByID(id: string): Observable<any>;
getObservableFilteredByID(id: string): import("rxjs/internal/Observable").Observable<any>;
/**
* connectToServer
*/
connectToServer(): Observable<any>;
connectToServer(): import("rxjs/internal/Observable").Observable<any>;
/**
* keepAlive, Ping and Pong to the Rocket.Chat Server to Keep the Connection Alive.
* Returns an Observable to subscribe to keepAlive, Ping and Pong to the Rocket.Chat Server to Keep the Connection Alive.
*/
keepAlive(): void;
keepAlive(): import("rxjs/internal/Observable").Observable<any>;
/**
* Login with Username and Password
*/
login(username: string, password: string): Observable<any>;
login(username: string, password: string): import("rxjs/internal/Observable").Observable<any>;
/**
* Login with Authentication Token
*/
loginWithAuthToken(authToken: string): Observable<any>;
loginWithAuthToken(authToken: string): import("rxjs/internal/Observable").Observable<any>;
/**
* Login with OAuth, with Client Token and Client Secret
*/
loginWithOAuth(credToken: string, credSecret: string): Observable<any>;
loginWithOAuth(credToken: string, credSecret: string): import("rxjs/internal/Observable").Observable<any>;
/**
* getLoginObservable
*/
getLoginObservable(id: string): Observable<any>;
getLoginObservable(id: string): import("rxjs/internal/Observable").Observable<any>;
/**
* Get Observalble to the Result of Method Call from Rocket.Chat Realtime API
*/
callMethod(method: string, ...params: Array<{}>): Observable<any>;
callMethod(method: string, ...params: Array<{}>): import("rxjs/internal/Observable").Observable<any>;
/**
* getSubscription
*/
getSubscription(streamName: string, streamParam: string, addEvent: boolean): Observable<any>;
getSubscription(streamName: string, streamParam: string, addEvent: boolean): import("rxjs/internal/Observable").Observable<any>;
}

@ -3,19 +3,18 @@
* Rocket.Chat RealTime API
*/
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require("rxjs");
var webSocket_1 = require("rxjs/webSocket");
var operators_1 = require("rxjs/operators");
var uuid_1 = require("uuid");
var crypto_js_1 = require("crypto-js");
var RealTimeAPI = /** @class */ (function () {
function RealTimeAPI(param) {
switch (typeof param) {
case "string":
this.url = param;
this.webSocket = rxjs_1.Observable.webSocket(this.url);
this.webSocket = webSocket_1.webSocket(param);
break;
case "object":
this.webSocket = param;
this.url = this.webSocket.url;
break;
default:
throw new Error("Invalid Parameter to the Constructor, Parameter must be of Type WebSocketSubject or URL but was found of type \"" + typeof param + "\"");
@ -25,7 +24,7 @@ var RealTimeAPI = /** @class */ (function () {
* Returns the Observable to the RealTime API Socket
*/
RealTimeAPI.prototype.getObservable = function () {
return this.webSocket.catch(function (err) { return rxjs_1.Observable.of(err); });
return this.webSocket;
};
/**
* Disconnect the WebSocket Connection between client and RealTime API
@ -61,33 +60,37 @@ var RealTimeAPI = /** @class */ (function () {
* sendMessage to Rocket.Chat Server
*/
RealTimeAPI.prototype.sendMessage = function (messageObject) {
this.webSocket.next(JSON.stringify(messageObject));
this.webSocket.next(messageObject);
};
/**
* getObservableFilteredByMessageType
*/
RealTimeAPI.prototype.getObservableFilteredByMessageType = function (messageType) {
return this.getObservable().filter(function (message) { return message.msg === messageType; });
return this.getObservable().pipe(operators_1.filter(function (message) { return message.msg === messageType; }));
};
/**
* getObservableFilteredByID
*/
RealTimeAPI.prototype.getObservableFilteredByID = function (id) {
return this.getObservable().filter(function (message) { return message.id === id; });
return this.getObservable().pipe(operators_1.filter(function (message) { return message.id === id; }));
};
/**
* connectToServer
*/
RealTimeAPI.prototype.connectToServer = function () {
this.sendMessage({ "msg": "connect", "version": "1", "support": ["1", "pre2", "pre1"] });
this.sendMessage({
msg: "connect",
version: "1",
support: ["1", "pre2", "pre1"]
});
return this.getObservableFilteredByMessageType("connected");
};
/**
* keepAlive, Ping and Pong to the Rocket.Chat Server to Keep the Connection Alive.
* Returns an Observable to subscribe to keepAlive, Ping and Pong to the Rocket.Chat Server to Keep the Connection Alive.
*/
RealTimeAPI.prototype.keepAlive = function () {
var _this = this;
this.getObservableFilteredByMessageType("ping").subscribe(function (message) { return _this.sendMessage({ msg: "pong" }); });
return this.getObservableFilteredByMessageType("ping").pipe(operators_1.tap(function () { return _this.sendMessage({ msg: "pong" }); }));
};
/**
* Login with Username and Password
@ -97,15 +100,15 @@ var RealTimeAPI = /** @class */ (function () {
var id = uuid_1.v4();
var usernameType = username.indexOf("@") !== -1 ? "email" : "username";
this.sendMessage({
"msg": "method",
"method": "login",
"id": id,
"params": [
msg: "method",
method: "login",
id: id,
params: [
{
"user": (_a = {}, _a[usernameType] = username, _a),
"password": {
"digest": crypto_js_1.SHA256(password).toString(),
"algorithm": "sha-256"
user: (_a = {}, _a[usernameType] = username, _a),
password: {
digest: crypto_js_1.SHA256(password).toString(),
algorithm: "sha-256"
}
}
]
@ -118,12 +121,10 @@ var RealTimeAPI = /** @class */ (function () {
RealTimeAPI.prototype.loginWithAuthToken = function (authToken) {
var id = uuid_1.v4();
this.sendMessage({
"msg": "method",
"method": "login",
"id": id,
"params": [
{ "resume": authToken }
]
msg: "method",
method: "login",
id: id,
params: [{ resume: authToken }]
});
return this.getLoginObservable(id);
};
@ -133,14 +134,14 @@ var RealTimeAPI = /** @class */ (function () {
RealTimeAPI.prototype.loginWithOAuth = function (credToken, credSecret) {
var id = uuid_1.v4();
this.sendMessage({
"msg": "method",
"method": "login",
"id": id,
"params": [
msg: "method",
method: "login",
id: id,
params: [
{
"oauth": {
"credentialToken": credToken,
"credentialSecret": credSecret
oauth: {
credentialToken: credToken,
credentialSecret: credSecret
}
}
]
@ -153,18 +154,18 @@ var RealTimeAPI = /** @class */ (function () {
RealTimeAPI.prototype.getLoginObservable = function (id) {
var resultObservable = this.getObservableFilteredByID(id);
var resultId;
var addedObservable = this.getObservable()
.buffer(resultObservable.map(function (_a) {
var addedObservable = this.getObservable().pipe(operators_1.buffer(resultObservable.pipe(operators_1.map(function (_a) {
var msg = _a.msg, error = _a.error, result = _a.result;
if (msg === "result" && !error)
return resultId = result.id; // Setting resultId to get Result from the buffer
}))
.flatMap(function (x) { return x; }) // Flattening the Buffered Messages
.filter(function (_a) {
return (resultId = result.id); // Setting resultId to get Result from the buffer
}))), operators_1.flatMap(function (x) { return x; }), // Flattening the Buffered Messages
operators_1.filter(function (_a) {
var msgId = _a.id;
return resultId !== undefined && msgId === resultId;
}); //Filtering the "added" result message.
return rxjs_1.Observable.merge(resultObservable, addedObservable); //Merging "result" and "added" messages.
}), //Filtering the "added" result message.
operators_1.merge(resultObservable) //Merging "result" and "added" messages.
);
return addedObservable;
};
/**
* Get Observalble to the Result of Method Call from Rocket.Chat Realtime API
@ -176,7 +177,7 @@ var RealTimeAPI = /** @class */ (function () {
}
var id = uuid_1.v4();
this.sendMessage({
"msg": "method",
msg: "method",
method: method,
id: id,
params: params
@ -188,18 +189,19 @@ var RealTimeAPI = /** @class */ (function () {
*/
RealTimeAPI.prototype.getSubscription = function (streamName, streamParam, addEvent) {
var id = uuid_1.v4();
var subscription = this.webSocket.multiplex(function () { return JSON.stringify({
"msg": "sub",
"id": id,
"name": streamName,
"params": [
streamParam,
addEvent
]
}); }, function () { return JSON.stringify({
"msg": "unsub",
"id": id
}); }, function (message) { return typeof message.collection === "string" && message.collection === streamName && message.fields.eventName === streamParam; } // Proper Filtering to be done. This is temporary filter just for the stream-room-messages subscription
var subscription = this.webSocket.multiplex(function () { return ({
msg: "sub",
id: id,
name: streamName,
params: [streamParam, addEvent]
}); }, function () { return ({
msg: "unsub",
id: id
}); }, function (message) {
return typeof message.collection === "string" &&
message.collection === streamName &&
message.fields.eventName === streamParam;
} // Proper Filtering to be done. This is temporary filter just for the stream-room-messages subscription
);
return subscription;
};

@ -1 +1 @@
{"version":3,"file":"RealTimeAPI.js","sourceRoot":"","sources":["../src/RealTimeAPI.ts"],"names":[],"mappings":";AAAA;;G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1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"aproba": {
"version": "1.2.0",
@ -3094,12 +3095,14 @@
"balanced-match": {
"version": "1.0.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
@ -3114,17 +3117,20 @@
"code-point-at": {
"version": "1.1.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"core-util-is": {
"version": "1.0.2",
@ -3241,7 +3247,8 @@
"inherits": {
"version": "2.0.3",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"ini": {
"version": "1.3.5",
@ -3253,6 +3260,7 @@
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
@ -3267,6 +3275,7 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
@ -3274,12 +3283,14 @@
"minimist": {
"version": "0.0.8",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"minipass": {
"version": "2.3.5",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"safe-buffer": "^5.1.2",
"yallist": "^3.0.0"
@ -3298,6 +3309,7 @@
"version": "0.5.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minimist": "0.0.8"
}
@ -3378,7 +3390,8 @@
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"object-assign": {
"version": "4.1.1",
@ -3390,6 +3403,7 @@
"version": "1.4.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"wrappy": "1"
}
@ -3475,7 +3489,8 @@
"safe-buffer": {
"version": "5.1.2",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"safer-buffer": {
"version": "2.1.2",
@ -3511,6 +3526,7 @@
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
@ -3530,6 +3546,7 @@
"version": "3.0.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"ansi-regex": "^2.0.0"
}
@ -3573,12 +3590,14 @@
"wrappy": {
"version": "1.0.2",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"yallist": {
"version": "3.0.3",
"bundled": true,
"dev": true
"dev": true,
"optional": true
}
}
},
@ -10265,11 +10284,11 @@
"dev": true
},
"rxjs": {
"version": "5.5.12",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-5.5.12.tgz",
"integrity": "sha512-xx2itnL5sBbqeeiVgNPVuQQ1nC8Jp2WfNJhXWHmElW9YmrpS9UVnNzhP3EH3HFqexO5Tlp8GhYY+WEcqcVMvGw==",
"version": "6.4.0",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.4.0.tgz",
"integrity": "sha512-Z9Yfa11F6B9Sg/BK9MnqnQ+aQYicPLtilXBp2yUtDt2JRCE0h26d33EnfO3ZxoNxG0T92OUucP3Ct7cpfkdFfw==",
"requires": {
"symbol-observable": "1.0.1"
"tslib": "^1.9.0"
}
},
"safe-buffer": {
@ -11388,11 +11407,6 @@
}
}
},
"symbol-observable": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-1.0.1.tgz",
"integrity": "sha1-g0D8RwLDEi310iKI+IKD9RPT/dQ="
},
"symbol-tree": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.2.tgz",
@ -11820,8 +11834,7 @@
"tslib": {
"version": "1.9.3",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz",
"integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==",
"dev": true
"integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ=="
},
"tunnel-agent": {
"version": "0.6.0",

@ -8,7 +8,7 @@
},
"dependencies": {
"crypto-js": "^3.1.9-1",
"rxjs": "^5.4.1",
"rxjs": "^6.4.0",
"uuid": "^3.1.0"
},
"devDependencies": {
@ -72,4 +72,4 @@
"**/__tests__/*.+(ts|tsx|js)"
]
}
}
}

@ -2,24 +2,21 @@
* Rocket.Chat RealTime API
*/
import { Observable } from "rxjs";
import { WebSocketSubject } from "rxjs/observable/dom/WebSocketSubject";
import { webSocket, WebSocketSubject } from "rxjs/webSocket";
import { filter, buffer, flatMap, merge, map, tap } from "rxjs/operators";
import { v4 as uuid } from "uuid";
import { SHA256 } from "crypto-js";
export class RealTimeAPI {
public url: string;
public webSocket: WebSocketSubject<{}>;
public webSocket: WebSocketSubject<any>;
constructor(param: string | WebSocketSubject<{}>) {
constructor(param: string | WebSocketSubject<any>) {
switch (typeof param) {
case "string":
this.url = param as string;
this.webSocket = Observable.webSocket(this.url);
this.webSocket = webSocket(param);
break;
case "object":
this.webSocket = param as WebSocketSubject<{}>;
this.url = this.webSocket.url;
this.webSocket = param as WebSocketSubject<any>;
break;
default:
throw new Error(
@ -32,7 +29,7 @@ export class RealTimeAPI {
* Returns the Observable to the RealTime API Socket
*/
public getObservable() {
return this.webSocket.catch(err => Observable.of(err));
return this.webSocket;
}
/**
@ -81,16 +78,16 @@ export class RealTimeAPI {
/**
* sendMessage to Rocket.Chat Server
*/
public sendMessage(messageObject: {}): void {
this.webSocket.next(JSON.stringify(messageObject));
public sendMessage(messageObject: any): void {
this.webSocket.next(messageObject);
}
/**
* getObservableFilteredByMessageType
*/
public getObservableFilteredByMessageType(messageType: string) {
return this.getObservable().filter(
(message: any) => message.msg === messageType
return this.getObservable().pipe(
filter((message: any) => message.msg === messageType)
);
}
@ -98,7 +95,9 @@ export class RealTimeAPI {
* getObservableFilteredByID
*/
public getObservableFilteredByID(id: string) {
return this.getObservable().filter((message: any) => message.id === id);
return this.getObservable().pipe(
filter((message: any) => message.id === id)
);
}
/**
@ -114,11 +113,11 @@ export class RealTimeAPI {
}
/**
* keepAlive, Ping and Pong to the Rocket.Chat Server to Keep the Connection Alive.
* Returns an Observable to subscribe to keepAlive, Ping and Pong to the Rocket.Chat Server to Keep the Connection Alive.
*/
public keepAlive(): void {
this.getObservableFilteredByMessageType("ping").subscribe(message =>
this.sendMessage({ msg: "pong" })
public keepAlive() {
return this.getObservableFilteredByMessageType("ping").pipe(
tap(() => this.sendMessage({ msg: "pong" }))
);
}
@ -187,16 +186,20 @@ export class RealTimeAPI {
let resultObservable = this.getObservableFilteredByID(id);
let resultId: string;
let addedObservable = this.getObservable()
.buffer(
resultObservable.map(({ msg, error, result }) => {
if (msg === "result" && !error) return (resultId = result.id); // Setting resultId to get Result from the buffer
})
)
.flatMap(x => x) // Flattening the Buffered Messages
.filter(({ id: msgId }) => resultId !== undefined && msgId === resultId); //Filtering the "added" result message.
let addedObservable = this.getObservable().pipe(
buffer(
resultObservable.pipe(
map(({ msg, error, result }) => {
if (msg === "result" && !error) return (resultId = result.id); // Setting resultId to get Result from the buffer
})
)
),
flatMap(x => x), // Flattening the Buffered Messages
filter(({ id: msgId }) => resultId !== undefined && msgId === resultId), //Filtering the "added" result message.
merge(resultObservable) //Merging "result" and "added" messages.
);
return Observable.merge(resultObservable, addedObservable); //Merging "result" and "added" messages.
return addedObservable;
}
/**
@ -223,18 +226,16 @@ export class RealTimeAPI {
) {
let id = uuid();
let subscription = this.webSocket.multiplex(
() =>
JSON.stringify({
msg: "sub",
id: id,
name: streamName,
params: [streamParam, addEvent]
}),
() =>
JSON.stringify({
msg: "unsub",
id: id
}),
() => ({
msg: "sub",
id: id,
name: streamName,
params: [streamParam, addEvent]
}),
() => ({
msg: "unsub",
id: id
}),
(message: any) =>
typeof message.collection === "string" &&
message.collection === streamName &&

Loading…
Cancel
Save