diff --git a/README.md b/README.md index 9dad88766..fbaba27a9 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,7 @@ ReactDOM.render( - `url: string` : url that the client will connect to, starts with `ws://` or `wss://` - `options?: Object` : optional, object to modify default client behavior * `timeout?: number` : how long the client should wait in ms for a keep-alive message from the server (default 30000 ms), this parameter is ignored if the server does not send keep-alive messages. This will also be used to calculate the max connection time per connect/reconnect + * `minTimeout?: number`: the minimum amount of time the client should wait for a connection to be made (default 1000 ms) * `lazy?: boolean` : use to set lazy mode - connects only when first subscription created, and delay the socket initialization * `connectionParams?: Object | Function | Promise` : object that will be available as first argument of `onConnect` (in server side), if passed a function - it will call it and send the return value, if function returns as promise - it will wait until it resolves and send the resolved value. * `reconnect?: boolean` : automatic reconnect in case of connection error @@ -277,7 +278,7 @@ ReactDOM.render( - => Returns an `off` method to cancel the event subscription. #### `onReconnecting(callback, thisContext) => Function` - shorthand for `.on('reconnecting', ...)` -- `callback: Function`: function to be called when websocket starts it's reconnection +- `callback: Function`: function to be called when websocket starts it's reconnection - `thisContext: any`: `this` context to use when calling the callback function. - => Returns an `off` method to cancel the event subscription. diff --git a/src/client.ts b/src/client.ts index 459d67d26..f8dc41c89 100644 --- a/src/client.ts +++ b/src/client.ts @@ -13,7 +13,7 @@ import { getOperationAST } from 'graphql/utilities/getOperationAST'; import $$observable from 'symbol-observable'; import { GRAPHQL_WS } from './protocol'; -import { WS_TIMEOUT } from './defaults'; +import { MIN_WS_TIMEOUT, WS_TIMEOUT } from './defaults'; import MessageTypes from './message-types'; export interface Observer { @@ -60,6 +60,7 @@ export type ConnectionParamsOptions = ConnectionParams | Function | Promise; // queued messages while websocket is opening. private reconnect: boolean; @@ -104,6 +106,7 @@ export class SubscriptionClient { const { connectionCallback = undefined, connectionParams = {}, + minTimeout = MIN_WS_TIMEOUT, timeout = WS_TIMEOUT, reconnect = false, reconnectionAttempts = Infinity, @@ -121,6 +124,7 @@ export class SubscriptionClient { this.url = url; this.operations = {}; this.nextOperationId = 0; + this.minWsTimeout = minTimeout; this.wsTimeout = timeout; this.unsentMessagesQueue = []; this.reconnect = reconnect; @@ -349,7 +353,7 @@ export class SubscriptionClient { } private createMaxConnectTimeGenerator() { - const minValue = 1000; + const minValue = this.minWsTimeout; const maxValue = this.wsTimeout; return new Backoff({ diff --git a/src/defaults.ts b/src/defaults.ts index b1ec67817..d744b7e32 100644 --- a/src/defaults.ts +++ b/src/defaults.ts @@ -1,5 +1,7 @@ +const MIN_WS_TIMEOUT = 1000; const WS_TIMEOUT = 30000; export { + MIN_WS_TIMEOUT, WS_TIMEOUT, };