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) [![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) [![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/) 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 ## Installation
``` ```
npm install --save rocket.chat.realtime.api.rxjs 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"); const realTimeAPI = new RealTimeAPI("wss://demo.rocket.chat/websocket");
// Provide, URL to the Rocket.Chat's Realtime API. // 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. // Responds "pong" to the "ping" message sent by the Realtime API. To keep the connection alive.
const auth = realTimeApi.login(USERNAME, PASSWORD); const auth = realTimeApi.login(USERNAME, PASSWORD);
@ -43,22 +43,21 @@ const auth = realTimeApi.login(USERNAME, PASSWORD);
## Methods ## Methods
| Methods | Functionality | | Methods | Functionality |
|------------------------------------------------------------ |----------------------------------------------------------------------------------------------- | | ---------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| connectToServer() | Initiates Connections to the Server to the RealTime API. Returns Observable with the server's response | | 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. | | 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. | | 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. | | 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. | | 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 | | 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 | | sendMessage(jsonObject) | Sends the JSON Object to the API Server |
| onMessage( message => console.log(message) ) | Subscribes to the Messages sent from the server | | onMessage( message => console.log(message) ) | Subscribes to the Messages sent from the server |
| onError( error => console.error(error) ) | Subscribes to the Errors. | | onError( error => console.error(error) ) | Subscribes to the Errors. |
| onCompletion(() => console.info("Complete")) | Subscribes to Completion on the Websocket Connection | | 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 | | 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 | | getObservable() | Returns observable of the WebSocket Connection to the RealTime API |
| disconnect() | Disconnect the WebSocket Connection between client and 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. ### 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 * Rocket.Chat RealTime API
*/ */
import { Observable } from "rxjs"; import { WebSocketSubject } from "rxjs/webSocket";
import { WebSocketSubject } from 'rxjs/observable/dom/WebSocketSubject';
export declare class RealTimeAPI { export declare class RealTimeAPI {
url: string; webSocket: WebSocketSubject<any>;
webSocket: WebSocketSubject<{}>; constructor(param: string | WebSocketSubject<any>);
constructor(param: string | WebSocketSubject<{}>);
/** /**
* Returns the Observable to the RealTime API Socket * Returns the Observable to the RealTime API Socket
*/ */
getObservable(): Observable<any>; getObservable(): WebSocketSubject<any>;
/** /**
* Disconnect the WebSocket Connection between client and RealTime API * Disconnect the WebSocket Connection between client and RealTime API
*/ */
@ -34,45 +32,45 @@ export declare class RealTimeAPI {
/** /**
* sendMessage to Rocket.Chat Server * sendMessage to Rocket.Chat Server
*/ */
sendMessage(messageObject: {}): void; sendMessage(messageObject: any): void;
/** /**
* getObservableFilteredByMessageType * getObservableFilteredByMessageType
*/ */
getObservableFilteredByMessageType(messageType: string): Observable<any>; getObservableFilteredByMessageType(messageType: string): import("rxjs/internal/Observable").Observable<any>;
/** /**
* getObservableFilteredByID * getObservableFilteredByID
*/ */
getObservableFilteredByID(id: string): Observable<any>; getObservableFilteredByID(id: string): import("rxjs/internal/Observable").Observable<any>;
/** /**
* connectToServer * 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 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 * 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 * 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
*/ */
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 * 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
*/ */
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 * Rocket.Chat RealTime API
*/ */
Object.defineProperty(exports, "__esModule", { value: true }); 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 uuid_1 = require("uuid");
var crypto_js_1 = require("crypto-js"); var crypto_js_1 = require("crypto-js");
var RealTimeAPI = /** @class */ (function () { var RealTimeAPI = /** @class */ (function () {
function RealTimeAPI(param) { function RealTimeAPI(param) {
switch (typeof param) { switch (typeof param) {
case "string": case "string":
this.url = param; this.webSocket = webSocket_1.webSocket(param);
this.webSocket = rxjs_1.Observable.webSocket(this.url);
break; break;
case "object": case "object":
this.webSocket = param; this.webSocket = param;
this.url = this.webSocket.url;
break; break;
default: default:
throw new Error("Invalid Parameter to the Constructor, Parameter must be of Type WebSocketSubject or URL but was found of type \"" + typeof param + "\""); 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 * Returns the Observable to the RealTime API Socket
*/ */
RealTimeAPI.prototype.getObservable = function () { 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 * Disconnect the WebSocket Connection between client and RealTime API
@ -61,33 +60,37 @@ var RealTimeAPI = /** @class */ (function () {
* sendMessage to Rocket.Chat Server * sendMessage to Rocket.Chat Server
*/ */
RealTimeAPI.prototype.sendMessage = function (messageObject) { RealTimeAPI.prototype.sendMessage = function (messageObject) {
this.webSocket.next(JSON.stringify(messageObject)); this.webSocket.next(messageObject);
}; };
/** /**
* getObservableFilteredByMessageType * getObservableFilteredByMessageType
*/ */
RealTimeAPI.prototype.getObservableFilteredByMessageType = function (messageType) { 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 * getObservableFilteredByID
*/ */
RealTimeAPI.prototype.getObservableFilteredByID = function (id) { 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 * connectToServer
*/ */
RealTimeAPI.prototype.connectToServer = function () { 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"); 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 () { RealTimeAPI.prototype.keepAlive = function () {
var _this = this; 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 * Login with Username and Password
@ -97,15 +100,15 @@ var RealTimeAPI = /** @class */ (function () {
var id = uuid_1.v4(); var id = uuid_1.v4();
var usernameType = username.indexOf("@") !== -1 ? "email" : "username"; var usernameType = username.indexOf("@") !== -1 ? "email" : "username";
this.sendMessage({ this.sendMessage({
"msg": "method", msg: "method",
"method": "login", method: "login",
"id": id, id: id,
"params": [ params: [
{ {
"user": (_a = {}, _a[usernameType] = username, _a), user: (_a = {}, _a[usernameType] = username, _a),
"password": { password: {
"digest": crypto_js_1.SHA256(password).toString(), digest: crypto_js_1.SHA256(password).toString(),
"algorithm": "sha-256" algorithm: "sha-256"
} }
} }
] ]
@ -118,12 +121,10 @@ var RealTimeAPI = /** @class */ (function () {
RealTimeAPI.prototype.loginWithAuthToken = function (authToken) { RealTimeAPI.prototype.loginWithAuthToken = function (authToken) {
var id = uuid_1.v4(); var id = uuid_1.v4();
this.sendMessage({ this.sendMessage({
"msg": "method", msg: "method",
"method": "login", method: "login",
"id": id, id: id,
"params": [ params: [{ resume: authToken }]
{ "resume": authToken }
]
}); });
return this.getLoginObservable(id); return this.getLoginObservable(id);
}; };
@ -133,14 +134,14 @@ var RealTimeAPI = /** @class */ (function () {
RealTimeAPI.prototype.loginWithOAuth = function (credToken, credSecret) { RealTimeAPI.prototype.loginWithOAuth = function (credToken, credSecret) {
var id = uuid_1.v4(); var id = uuid_1.v4();
this.sendMessage({ this.sendMessage({
"msg": "method", msg: "method",
"method": "login", method: "login",
"id": id, id: id,
"params": [ params: [
{ {
"oauth": { oauth: {
"credentialToken": credToken, credentialToken: credToken,
"credentialSecret": credSecret credentialSecret: credSecret
} }
} }
] ]
@ -153,18 +154,18 @@ var RealTimeAPI = /** @class */ (function () {
RealTimeAPI.prototype.getLoginObservable = function (id) { RealTimeAPI.prototype.getLoginObservable = function (id) {
var resultObservable = this.getObservableFilteredByID(id); var resultObservable = this.getObservableFilteredByID(id);
var resultId; var resultId;
var addedObservable = this.getObservable() var addedObservable = this.getObservable().pipe(operators_1.buffer(resultObservable.pipe(operators_1.map(function (_a) {
.buffer(resultObservable.map(function (_a) {
var msg = _a.msg, error = _a.error, result = _a.result; var msg = _a.msg, error = _a.error, result = _a.result;
if (msg === "result" && !error) if (msg === "result" && !error)
return resultId = result.id; // Setting resultId to get Result from the buffer return (resultId = result.id); // Setting resultId to get Result from the buffer
})) }))), operators_1.flatMap(function (x) { return x; }), // Flattening the Buffered Messages
.flatMap(function (x) { return x; }) // Flattening the Buffered Messages operators_1.filter(function (_a) {
.filter(function (_a) {
var msgId = _a.id; var msgId = _a.id;
return resultId !== undefined && msgId === resultId; return resultId !== undefined && msgId === resultId;
}); //Filtering the "added" result message. }), //Filtering the "added" result message.
return rxjs_1.Observable.merge(resultObservable, addedObservable); //Merging "result" and "added" messages. operators_1.merge(resultObservable) //Merging "result" and "added" messages.
);
return addedObservable;
}; };
/** /**
* Get Observalble to the Result of Method Call from Rocket.Chat Realtime API * 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(); var id = uuid_1.v4();
this.sendMessage({ this.sendMessage({
"msg": "method", msg: "method",
method: method, method: method,
id: id, id: id,
params: params params: params
@ -188,18 +189,19 @@ var RealTimeAPI = /** @class */ (function () {
*/ */
RealTimeAPI.prototype.getSubscription = function (streamName, streamParam, addEvent) { RealTimeAPI.prototype.getSubscription = function (streamName, streamParam, addEvent) {
var id = uuid_1.v4(); var id = uuid_1.v4();
var subscription = this.webSocket.multiplex(function () { return JSON.stringify({ var subscription = this.webSocket.multiplex(function () { return ({
"msg": "sub", msg: "sub",
"id": id, id: id,
"name": streamName, name: streamName,
"params": [ params: [streamParam, addEvent]
streamParam, }); }, function () { return ({
addEvent msg: "unsub",
] id: id
}); }, function () { return JSON.stringify({ }); }, function (message) {
"msg": "unsub", return typeof message.collection === "string" &&
"id": id message.collection === streamName &&
}); }, 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 message.fields.eventName === streamParam;
} // Proper Filtering to be done. This is temporary filter just for the stream-room-messages subscription
); );
return subscription; return subscription;
}; };

@ -1 +1 @@
{"version":3,"file":"RealTimeAPI.js","sourceRoot":"","sources":["../src/RealTimeAPI.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAEH,6BAAkC;AAElC,6BAAkC;AAClC,uCAAmC;AAEnC;IAII,qBAAY,KAAoC;QAC5C,QAAQ,OAAO,KAAK,EAAE;YAClB,KAAK,QAAQ;gBACT,IAAI,CAAC,GAAG,GAAG,KAAe,CAAC;gBAC3B,IAAI,CAAC,SAAS,GAAG,iBAAU,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBAChD,MAAM;YACV,KAAK,QAAQ;gBACT,IAAI,CAAC,SAAS,GAAG,KAA6B,CAAC;gBAC/C,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC;gBAC9B,MAAM;YACV;gBACI,MAAM,IAAI,KAAK,CAAC,qHAAkH,OAAO,KAAK,OAAG,CAAC,CAAC;SAC1J;IACL,CAAC;IAED;;OAEG;IACI,mCAAa,GAApB;QAC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B,IAAI,EAAE,GAAG,SAAI,EAAE,CAAC;QAChB,IAAI,YAAY,GAAG,IAAI,CAAC,SAAS,CAAC,SAAS,CACzC,cAAM,OAAA,CAAC;YACL,GAAG,EAAE,KAAK;YACV,EAAE,EAAE,EAAE;YACN,IAAI,EAAE,UAAU;YAChB,MAAM,EAAE,CAAC,WAAW,EAAE,QAAQ,CAAC;SAChC,CAAC,EALI,CAKJ,EACF,cAAM,OAAA,CAAC;YACL,GAAG,EAAE,OAAO;YACZ,EAAE,EAAE,EAAE;SACP,CAAC,EAHI,CAGJ,EACF,UAAC,OAAY;YACX,OAAA,OAAO,OAAO,CAAC,UAAU,KAAK,QAAQ;gBACtC,OAAO,CAAC,UAAU,KAAK,UAAU;gBACjC,OAAO,CAAC,MAAM,CAAC,SAAS,KAAK,WAAW;QAFxC,CAEwC,CAAC,uGAAuG;SACnJ,CAAC;QACF,OAAO,YAAY,CAAC;IACtB,CAAC;IACH,kBAAC;AAAD,CAAC,AA5OD,IA4OC;AA5OY,kCAAW"}

57
package-lock.json generated

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

@ -8,7 +8,7 @@
}, },
"dependencies": { "dependencies": {
"crypto-js": "^3.1.9-1", "crypto-js": "^3.1.9-1",
"rxjs": "^5.4.1", "rxjs": "^6.4.0",
"uuid": "^3.1.0" "uuid": "^3.1.0"
}, },
"devDependencies": { "devDependencies": {

@ -2,24 +2,21 @@
* Rocket.Chat RealTime API * Rocket.Chat RealTime API
*/ */
import { Observable } from "rxjs"; import { webSocket, WebSocketSubject } from "rxjs/webSocket";
import { WebSocketSubject } from "rxjs/observable/dom/WebSocketSubject"; import { filter, buffer, flatMap, merge, map, tap } from "rxjs/operators";
import { v4 as uuid } from "uuid"; import { v4 as uuid } from "uuid";
import { SHA256 } from "crypto-js"; import { SHA256 } from "crypto-js";
export class RealTimeAPI { export class RealTimeAPI {
public url: string; public webSocket: WebSocketSubject<any>;
public webSocket: WebSocketSubject<{}>;
constructor(param: string | WebSocketSubject<{}>) { constructor(param: string | WebSocketSubject<any>) {
switch (typeof param) { switch (typeof param) {
case "string": case "string":
this.url = param as string; this.webSocket = webSocket(param);
this.webSocket = Observable.webSocket(this.url);
break; break;
case "object": case "object":
this.webSocket = param as WebSocketSubject<{}>; this.webSocket = param as WebSocketSubject<any>;
this.url = this.webSocket.url;
break; break;
default: default:
throw new Error( throw new Error(
@ -32,7 +29,7 @@ export class RealTimeAPI {
* Returns the Observable to the RealTime API Socket * Returns the Observable to the RealTime API Socket
*/ */
public getObservable() { 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 * sendMessage to Rocket.Chat Server
*/ */
public sendMessage(messageObject: {}): void { public sendMessage(messageObject: any): void {
this.webSocket.next(JSON.stringify(messageObject)); this.webSocket.next(messageObject);
} }
/** /**
* getObservableFilteredByMessageType * getObservableFilteredByMessageType
*/ */
public getObservableFilteredByMessageType(messageType: string) { public getObservableFilteredByMessageType(messageType: string) {
return this.getObservable().filter( return this.getObservable().pipe(
(message: any) => message.msg === messageType filter((message: any) => message.msg === messageType)
); );
} }
@ -98,7 +95,9 @@ export class RealTimeAPI {
* getObservableFilteredByID * getObservableFilteredByID
*/ */
public getObservableFilteredByID(id: string) { 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 { public keepAlive() {
this.getObservableFilteredByMessageType("ping").subscribe(message => return this.getObservableFilteredByMessageType("ping").pipe(
this.sendMessage({ msg: "pong" }) tap(() => this.sendMessage({ msg: "pong" }))
); );
} }
@ -187,16 +186,20 @@ export class RealTimeAPI {
let resultObservable = this.getObservableFilteredByID(id); let resultObservable = this.getObservableFilteredByID(id);
let resultId: string; let resultId: string;
let addedObservable = this.getObservable() let addedObservable = this.getObservable().pipe(
.buffer( buffer(
resultObservable.map(({ msg, error, result }) => { resultObservable.pipe(
if (msg === "result" && !error) return (resultId = result.id); // Setting resultId to get Result from the buffer 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. ),
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 id = uuid();
let subscription = this.webSocket.multiplex( let subscription = this.webSocket.multiplex(
() => () => ({
JSON.stringify({ msg: "sub",
msg: "sub", id: id,
id: id, name: streamName,
name: streamName, params: [streamParam, addEvent]
params: [streamParam, addEvent] }),
}), () => ({
() => msg: "unsub",
JSON.stringify({ id: id
msg: "unsub", }),
id: id
}),
(message: any) => (message: any) =>
typeof message.collection === "string" && typeof message.collection === "string" &&
message.collection === streamName && message.collection === streamName &&

Loading…
Cancel
Save