Skip to content

Commit d925f4e

Browse files
authored
#77: Support Custom WebSocket URL (#78)
* #77: add custom websocket URL to listener config * #77: build * #77: bump version in doc * #77: update jsdoc default value * 1.0.2 * update README version
1 parent 8222238 commit d925f4e

File tree

9 files changed

+28
-14
lines changed

9 files changed

+28
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ npm install tesjs
2323
```
2424
Or in browsers through a CDN
2525
```html
26-
<script src="https://cdn.jsdelivr.net/gh/mitchwadair/tesjs@v1.0.1/dist/tes.min.js"></script>
26+
<script src="https://cdn.jsdelivr.net/gh/mitchwadair/tesjs@v1.0.2/dist/tes.min.js"></script>
2727
```
2828

2929
# Basic Usage
@@ -64,7 +64,7 @@ tes.subscribe("channel.update", { broadcaster_user_id: "1337" })
6464
# Browser
6565
TESjs supports WebSocket transport, and can be used in a browser environment
6666
```html
67-
<script src="https://cdn.jsdelivr.net/gh/mitchwadair/tesjs@v1.0.1/dist/tes.min.js"></script>
67+
<script src="https://cdn.jsdelivr.net/gh/mitchwadair/tesjs@v1.0.2/dist/tes.min.js"></script>
6868
<script>
6969
const config = {
7070
identity: {

dist/tes.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/tes.min.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ npm install tesjs
88
```
99
Or in browsers through a CDN
1010
```html
11-
<script src="https://cdn.jsdelivr.net/gh/mitchwadair/tesjs@v1.0.1/dist/tes.min.js"></script>
11+
<script src="https://cdn.jsdelivr.net/gh/mitchwadair/tesjs@v1.0.2/dist/tes.min.js"></script>
1212
```
1313

1414
# Quick Links

doc/tesjs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ Listener configuration
347347
| --- | --- | --- | --- |
348348
| type | <code>&quot;webhook&quot;</code> \| <code>&quot;websocket&quot;</code> | | The type of transport to use |
349349
| [baseURL] | <code>string</code> | | Required for `webhook` transport. The base URL where your app is hosted. See [Twitch doc](https://dev.twitch.tv/docs/eventsub) for details on local development |
350+
| [websocketURL] | <code>string</code> | <code>&quot;wss://eventsub.wss.twitch.tv/ws&quot;</code> | A custom websocket URL to use for `websocket` transport. Useful for local testing with [Twitch CLI](https://dev.twitch.tv/docs/cli/) |
350351
| [secret] | <code>string</code> | | Required for `webhook` transport. The secret to use for your `webhook` subscriptions. Should be different from your client secret |
351352
| [server] | <code>Express</code> | | The Express app object. Use if integrating with an existing Express app |
352353
| [port] | <code>number</code> | <code>process.env.PORT,8080</code> | A custom port to use |

lib/tes.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ const SUBS_API_URL = "https://api.twitch.tv/helix/eventsub/subscriptions";
6262
* @param {"webhook"|"websocket"} type The type of transport to use
6363
* @param {string} [baseURL] Required for `webhook` transport. The base URL where your app is
6464
* hosted. See [Twitch doc](https://dev.twitch.tv/docs/eventsub) for details on local development
65+
* @param {string} [websocketURL=wss://eventsub.wss.twitch.tv/ws] A custom websocket URL to use for `websocket` transport. Useful for
66+
* local testing with [Twitch CLI](https://dev.twitch.tv/docs/cli/)
6567
* @param {string} [secret] Required for `webhook` transport. The secret to use for your `webhook`
6668
* subscriptions. Should be different from your client secret
6769
* @param {Express} [server] The Express app object. Use if integrating with an existing Express app
@@ -123,7 +125,16 @@ class TES {
123125

124126
const {
125127
identity: { id, secret, onAuthenticationFailure, accessToken, refreshToken },
126-
listener: { type, baseURL, secret: whSecret, port, ignoreDuplicateMessages, ignoreOldMessages, server },
128+
listener: {
129+
type,
130+
baseURL,
131+
secret: whSecret,
132+
port,
133+
ignoreDuplicateMessages,
134+
ignoreOldMessages,
135+
server,
136+
websocketURL,
137+
},
127138
} = config;
128139

129140
if (!type || (type !== "webhook" && type !== "websocket")) {
@@ -161,7 +172,7 @@ class TES {
161172
this.whserver = whserver(server, whSecret, serverConfig);
162173
this._whserverlistener = server ? null : this.whserver.listen(this.port);
163174
} else {
164-
this.wsclient = new WebSocketClient();
175+
this.wsclient = new WebSocketClient(websocketURL);
165176
}
166177

167178
config.options = config.options || {};

lib/wsclient.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ const { objectShallowEquals } = require("./utils");
1212
const WS_URL = "wss://eventsub.wss.twitch.tv/ws";
1313

1414
class WebSocketClient {
15-
constructor() {
15+
constructor(wsURL) {
1616
// singleton
1717
if (WebSocketClient._instance) return WebSocketClient._instance;
1818
WebSocketClient._instance = this;
1919

2020
this._connections = {};
21+
22+
this._wsURL = wsURL || WS_URL;
2123
}
2224

2325
/**
@@ -83,7 +85,7 @@ class WebSocketClient {
8385
}
8486
}
8587

86-
_addConnection(onWelcome, url = WS_URL) {
88+
_addConnection(onWelcome, url = this._wsURL) {
8789
const ws = new WebSocket(url);
8890
ws.onmessage = (event) => {
8991
const {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tesjs",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A module to streamline the use of Twitch EventSub in Node.js and Web applications",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)