123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- /**
- * Copyright (c) 2015-present, Parse, LLC.
- * All rights reserved.
- *
- * This source code is licensed under the BSD-style license found in the
- * LICENSE file in the root directory of this source tree. An additional grant
- * of patent rights can be found in the PATENTS file in the same directory.
- *
- */
- /* global WebSocket */
- import CoreManager from './CoreManager';
- import EventEmitter from './EventEmitter';
- import ParseObject from './ParseObject';
- import LiveQuerySubscription from './LiveQuerySubscription';
- import { resolvingPromise } from './promiseUtils'; // The LiveQuery client inner state
- const CLIENT_STATE = {
- INITIALIZED: 'initialized',
- CONNECTING: 'connecting',
- CONNECTED: 'connected',
- CLOSED: 'closed',
- RECONNECTING: 'reconnecting',
- DISCONNECTED: 'disconnected'
- }; // The event type the LiveQuery client should sent to server
- const OP_TYPES = {
- CONNECT: 'connect',
- SUBSCRIBE: 'subscribe',
- UNSUBSCRIBE: 'unsubscribe',
- ERROR: 'error'
- }; // The event we get back from LiveQuery server
- const OP_EVENTS = {
- CONNECTED: 'connected',
- SUBSCRIBED: 'subscribed',
- UNSUBSCRIBED: 'unsubscribed',
- ERROR: 'error',
- CREATE: 'create',
- UPDATE: 'update',
- ENTER: 'enter',
- LEAVE: 'leave',
- DELETE: 'delete'
- }; // The event the LiveQuery client should emit
- const CLIENT_EMMITER_TYPES = {
- CLOSE: 'close',
- ERROR: 'error',
- OPEN: 'open'
- }; // The event the LiveQuery subscription should emit
- const SUBSCRIPTION_EMMITER_TYPES = {
- OPEN: 'open',
- CLOSE: 'close',
- ERROR: 'error',
- CREATE: 'create',
- UPDATE: 'update',
- ENTER: 'enter',
- LEAVE: 'leave',
- DELETE: 'delete'
- };
- const generateInterval = k => {
- return Math.random() * Math.min(30, Math.pow(2, k) - 1) * 1000;
- };
- /**
- * Creates a new LiveQueryClient.
- * Extends events.EventEmitter
- * <a href="https://nodejs.org/api/events.html#events_class_eventemitter">cloud functions</a>.
- *
- * A wrapper of a standard WebSocket client. We add several useful methods to
- * help you connect/disconnect to LiveQueryServer, subscribe/unsubscribe a ParseQuery easily.
- *
- * javascriptKey and masterKey are used for verifying the LiveQueryClient when it tries
- * to connect to the LiveQuery server
- *
- * We expose three events to help you monitor the status of the LiveQueryClient.
- *
- * <pre>
- * let Parse = require('parse/node');
- * let LiveQueryClient = Parse.LiveQueryClient;
- * let client = new LiveQueryClient({
- * applicationId: '',
- * serverURL: '',
- * javascriptKey: '',
- * masterKey: ''
- * });
- * </pre>
- *
- * Open - When we establish the WebSocket connection to the LiveQuery server, you'll get this event.
- * <pre>
- * client.on('open', () => {
- *
- * });</pre>
- *
- * Close - When we lose the WebSocket connection to the LiveQuery server, you'll get this event.
- * <pre>
- * client.on('close', () => {
- *
- * });</pre>
- *
- * Error - When some network error or LiveQuery server error happens, you'll get this event.
- * <pre>
- * client.on('error', (error) => {
- *
- * });</pre>
- * @alias Parse.LiveQueryClient
- */
- class LiveQueryClient extends EventEmitter {
- /*:: attempts: number;*/
- /*:: id: number;*/
- /*:: requestId: number;*/
- /*:: applicationId: string;*/
- /*:: serverURL: string;*/
- /*:: javascriptKey: ?string;*/
- /*:: masterKey: ?string;*/
- /*:: sessionToken: ?string;*/
- /*:: connectPromise: Promise;*/
- /*:: subscriptions: Map;*/
- /*:: socket: any;*/
- /*:: state: string;*/
- /**
- * @param {Object} options
- * @param {string} options.applicationId - applicationId of your Parse app
- * @param {string} options.serverURL - <b>the URL of your LiveQuery server</b>
- * @param {string} options.javascriptKey (optional)
- * @param {string} options.masterKey (optional) Your Parse Master Key. (Node.js only!)
- * @param {string} options.sessionToken (optional)
- */
- constructor({
- applicationId,
- serverURL,
- javascriptKey,
- masterKey,
- sessionToken
- }) {
- super();
- if (!serverURL || serverURL.indexOf('ws') !== 0) {
- throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
- }
- this.reconnectHandle = null;
- this.attempts = 1;
- this.id = 0;
- this.requestId = 1;
- this.serverURL = serverURL;
- this.applicationId = applicationId;
- this.javascriptKey = javascriptKey;
- this.masterKey = masterKey;
- this.sessionToken = sessionToken;
- this.connectPromise = resolvingPromise();
- this.subscriptions = new Map();
- this.state = CLIENT_STATE.INITIALIZED;
- }
- shouldOpen()
- /*: any*/
- {
- return this.state === CLIENT_STATE.INITIALIZED || this.state === CLIENT_STATE.DISCONNECTED;
- }
- /**
- * Subscribes to a ParseQuery
- *
- * If you provide the sessionToken, when the LiveQuery server gets ParseObject's
- * updates from parse server, it'll try to check whether the sessionToken fulfills
- * the ParseObject's ACL. The LiveQuery server will only send updates to clients whose
- * sessionToken is fit for the ParseObject's ACL. You can check the LiveQuery protocol
- * <a href="https://github.com/parse-community/parse-server/wiki/Parse-LiveQuery-Protocol-Specification">here</a> for more details. The subscription you get is the same subscription you get
- * from our Standard API.
- *
- * @param {Object} query - the ParseQuery you want to subscribe to
- * @param {string} sessionToken (optional)
- * @return {Object} subscription
- */
- subscribe(query
- /*: Object*/
- , sessionToken
- /*: ?string*/
- )
- /*: Object*/
- {
- if (!query) {
- return;
- }
- const className = query.className;
- const queryJSON = query.toJSON();
- const where = queryJSON.where;
- const fields = queryJSON.keys ? queryJSON.keys.split(',') : undefined;
- const subscribeRequest = {
- op: OP_TYPES.SUBSCRIBE,
- requestId: this.requestId,
- query: {
- className,
- where,
- fields
- }
- };
- if (sessionToken) {
- subscribeRequest.sessionToken = sessionToken;
- }
- const subscription = new LiveQuerySubscription(this.requestId, query, sessionToken);
- this.subscriptions.set(this.requestId, subscription);
- this.requestId += 1;
- this.connectPromise.then(() => {
- this.socket.send(JSON.stringify(subscribeRequest));
- });
- return subscription;
- }
- /**
- * After calling unsubscribe you'll stop receiving events from the subscription object.
- *
- * @param {Object} subscription - subscription you would like to unsubscribe from.
- */
- unsubscribe(subscription
- /*: Object*/
- ) {
- if (!subscription) {
- return;
- }
- this.subscriptions.delete(subscription.id);
- const unsubscribeRequest = {
- op: OP_TYPES.UNSUBSCRIBE,
- requestId: subscription.id
- };
- this.connectPromise.then(() => {
- this.socket.send(JSON.stringify(unsubscribeRequest));
- });
- }
- /**
- * After open is called, the LiveQueryClient will try to send a connect request
- * to the LiveQuery server.
- *
- */
- open() {
- const WebSocketImplementation = CoreManager.getWebSocketController();
- if (!WebSocketImplementation) {
- this.emit(CLIENT_EMMITER_TYPES.ERROR, 'Can not find WebSocket implementation');
- return;
- }
- if (this.state !== CLIENT_STATE.RECONNECTING) {
- this.state = CLIENT_STATE.CONNECTING;
- }
- this.socket = new WebSocketImplementation(this.serverURL); // Bind WebSocket callbacks
- this.socket.onopen = () => {
- this._handleWebSocketOpen();
- };
- this.socket.onmessage = event => {
- this._handleWebSocketMessage(event);
- };
- this.socket.onclose = () => {
- this._handleWebSocketClose();
- };
- this.socket.onerror = error => {
- this._handleWebSocketError(error);
- };
- }
- resubscribe() {
- this.subscriptions.forEach((subscription, requestId) => {
- const query = subscription.query;
- const queryJSON = query.toJSON();
- const where = queryJSON.where;
- const fields = queryJSON.keys ? queryJSON.keys.split(',') : undefined;
- const className = query.className;
- const sessionToken = subscription.sessionToken;
- const subscribeRequest = {
- op: OP_TYPES.SUBSCRIBE,
- requestId,
- query: {
- className,
- where,
- fields
- }
- };
- if (sessionToken) {
- subscribeRequest.sessionToken = sessionToken;
- }
- this.connectPromise.then(() => {
- this.socket.send(JSON.stringify(subscribeRequest));
- });
- });
- }
- /**
- * This method will close the WebSocket connection to this LiveQueryClient,
- * cancel the auto reconnect and unsubscribe all subscriptions based on it.
- *
- */
- close() {
- if (this.state === CLIENT_STATE.INITIALIZED || this.state === CLIENT_STATE.DISCONNECTED) {
- return;
- }
- this.state = CLIENT_STATE.DISCONNECTED;
- this.socket.close(); // Notify each subscription about the close
- for (const subscription of this.subscriptions.values()) {
- subscription.emit(SUBSCRIPTION_EMMITER_TYPES.CLOSE);
- }
- this._handleReset();
- this.emit(CLIENT_EMMITER_TYPES.CLOSE);
- } // ensure we start with valid state if connect is called again after close
- _handleReset() {
- this.attempts = 1;
- this.id = 0;
- this.requestId = 1;
- this.connectPromise = resolvingPromise();
- this.subscriptions = new Map();
- }
- _handleWebSocketOpen() {
- this.attempts = 1;
- const connectRequest = {
- op: OP_TYPES.CONNECT,
- applicationId: this.applicationId,
- javascriptKey: this.javascriptKey,
- masterKey: this.masterKey,
- sessionToken: this.sessionToken
- };
- this.socket.send(JSON.stringify(connectRequest));
- }
- _handleWebSocketMessage(event
- /*: any*/
- ) {
- let data = event.data;
- if (typeof data === 'string') {
- data = JSON.parse(data);
- }
- let subscription = null;
- if (data.requestId) {
- subscription = this.subscriptions.get(data.requestId);
- }
- switch (data.op) {
- case OP_EVENTS.CONNECTED:
- if (this.state === CLIENT_STATE.RECONNECTING) {
- this.resubscribe();
- }
- this.emit(CLIENT_EMMITER_TYPES.OPEN);
- this.id = data.clientId;
- this.connectPromise.resolve();
- this.state = CLIENT_STATE.CONNECTED;
- break;
- case OP_EVENTS.SUBSCRIBED:
- if (subscription) {
- subscription.emit(SUBSCRIPTION_EMMITER_TYPES.OPEN);
- }
- break;
- case OP_EVENTS.ERROR:
- if (data.requestId) {
- if (subscription) {
- subscription.emit(SUBSCRIPTION_EMMITER_TYPES.ERROR, data.error);
- }
- } else {
- this.emit(CLIENT_EMMITER_TYPES.ERROR, data.error);
- }
- break;
- case OP_EVENTS.UNSUBSCRIBED:
- // We have already deleted subscription in unsubscribe(), do nothing here
- break;
- default:
- {
- // create, update, enter, leave, delete cases
- if (!subscription) {
- break;
- }
- let override = false;
- if (data.original) {
- override = true;
- delete data.original.__type; // Check for removed fields
- for (const field in data.original) {
- if (!(field in data.object)) {
- data.object[field] = undefined;
- }
- }
- data.original = ParseObject.fromJSON(data.original, false);
- }
- delete data.object.__type;
- const parseObject = ParseObject.fromJSON(data.object, override);
- subscription.emit(data.op, parseObject, data.original);
- const localDatastore = CoreManager.getLocalDatastore();
- if (override && localDatastore.isEnabled) {
- localDatastore._updateObjectIfPinned(parseObject).then(() => {});
- }
- }
- }
- }
- _handleWebSocketClose() {
- if (this.state === CLIENT_STATE.DISCONNECTED) {
- return;
- }
- this.state = CLIENT_STATE.CLOSED;
- this.emit(CLIENT_EMMITER_TYPES.CLOSE); // Notify each subscription about the close
- for (const subscription of this.subscriptions.values()) {
- subscription.emit(SUBSCRIPTION_EMMITER_TYPES.CLOSE);
- }
- this._handleReconnect();
- }
- _handleWebSocketError(error
- /*: any*/
- ) {
- this.emit(CLIENT_EMMITER_TYPES.ERROR, error);
- for (const subscription of this.subscriptions.values()) {
- subscription.emit(SUBSCRIPTION_EMMITER_TYPES.ERROR);
- }
- this._handleReconnect();
- }
- _handleReconnect() {
- // if closed or currently reconnecting we stop attempting to reconnect
- if (this.state === CLIENT_STATE.DISCONNECTED) {
- return;
- }
- this.state = CLIENT_STATE.RECONNECTING;
- const time = generateInterval(this.attempts); // handle case when both close/error occur at frequent rates we ensure we do not reconnect unnecessarily.
- // we're unable to distinguish different between close/error when we're unable to reconnect therefore
- // we try to reonnect in both cases
- // server side ws and browser WebSocket behave differently in when close/error get triggered
- if (this.reconnectHandle) {
- clearTimeout(this.reconnectHandle);
- }
- this.reconnectHandle = setTimeout((() => {
- this.attempts++;
- this.connectPromise = resolvingPromise();
- this.open();
- }).bind(this), time);
- }
- }
- CoreManager.setWebSocketController(WebSocket);
- export default LiveQueryClient;
|