Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Allow dynamic URL #847

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v0.9.19 (TDB)
- Allow for dynamic url [#847](https://github.com/apollographql/subscriptions-transport-ws/pull/847)

## v0.9.18 (2020-08-17)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ ReactDOM.render(

## SubscriptionClient
### `Constructor(url, options, webSocketImpl)`
- `url: string` : url that the client will connect to, starts with `ws://` or `wss://`
- `url: string | (() => string)` : url that the client will connect to, starts with `ws://` or `wss://` - can alternatively be a function that returns the connection url, which will be invoked every time the client tries to connect
- `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)
Expand Down
7 changes: 4 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface ClientOptions {
export class SubscriptionClient {
public client: any;
public operations: Operations;
private url: string;
private url: string | (() => string);
private nextOperationId: number;
private connectionParams: Function;
private minWsTimeout: number;
Expand All @@ -100,7 +100,7 @@ export class SubscriptionClient {
private wsOptionArguments: any[];

constructor(
url: string,
url: string | (() => string),
options?: ClientOptions,
webSocketImpl?: any,
webSocketProtocols?: string | string[],
Expand Down Expand Up @@ -554,7 +554,8 @@ export class SubscriptionClient {
}

private connect() {
this.client = new this.wsImpl(this.url, this.wsProtocols, ...this.wsOptionArguments);
const url = typeof this.url === 'string' ? this.url : this.url();
this.client = new this.wsImpl(url, this.wsProtocols, ...this.wsOptionArguments);

this.checkMaxConnectTimeout();

Expand Down