-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from mitchwadair/ws-dev
#33: Add WebSocket Transport Support (beta)
- Loading branch information
Showing
28 changed files
with
1,365 additions
and
391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,27 +8,22 @@ on: | |
types: [published] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: 12 | ||
- run: brew install twitchdev/twitch/twitch-cli | ||
- run: npm ci | ||
- run: npm test | ||
|
||
publish-npm: | ||
needs: build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v1 | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 12 | ||
node-version: 16 | ||
registry-url: https://registry.npmjs.org/ | ||
- uses: Saionaro/[email protected] | ||
- run: npm ci | ||
- run: npm publish | ||
- if: "contains(steps.extract_version.outputs.version, 'beta')" | ||
run: npm publish --tag beta | ||
env: | ||
NODE_AUTH_TOKEN: ${{secrets.npm_token}} | ||
- if: "!contains(steps.extract_version.outputs.version, 'beta')" | ||
run: npm publish | ||
env: | ||
NODE_AUTH_TOKEN: ${{secrets.npm_token}} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,5 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
|
||
# Diagnostic reports (https://nodejs.org/api/report.html) | ||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
*.lcov | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# TypeScript v1 declaration files | ||
typings/ | ||
|
||
# TypeScript cache | ||
*.tsbuildinfo | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Microbundle cache | ||
.rpt2_cache/ | ||
.rts2_cache_cjs/ | ||
.rts2_cache_es/ | ||
.rts2_cache_umd/ | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
.env.test | ||
|
||
# parcel-bundler cache (https://parceljs.org/) | ||
.cache | ||
|
||
# Next.js build output | ||
.next | ||
|
||
# Nuxt.js build / generate output | ||
.nuxt | ||
dist | ||
|
||
# Gatsby files | ||
.cache/ | ||
# Comment in the public line in if your project uses Gatsby and *not* Next.js | ||
# https://nextjs.org/blog/next-9-1#public-directory-support | ||
# public | ||
|
||
# vuepress build output | ||
.vuepress/dist | ||
|
||
# Serverless directories | ||
.serverless/ | ||
|
||
# FuseBox cache | ||
.fusebox/ | ||
|
||
# DynamoDB Local files | ||
.dynamodb/ | ||
|
||
# TernJS port file | ||
.tern-port | ||
# Test dirs | ||
test-browser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
package.json | ||
package.json | ||
*.md | ||
*.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,8 @@ | |
|
||
A module to streamline the use of Twitch EventSub in Node.js applications | ||
|
||
# WARNING | ||
For developers using TESjs versions less than v0.5.0, it is **HIGHLY** recommended to upgrade to at least that version. See [#34](https://github.com/mitchwadair/tesjs/issues/34) for details. This will require you to remake all current subscriptions made with TESjs in order for it to work. Many apologies for the inconvenience! | ||
# WebSockets now Available! | ||
WebSocket transport is now available in TESjs! You can use TESjs with WebSocket transport in your client and server-side applications. Keep in mind that the WebSocket transport is currently in [open beta](https://discuss.dev.twitch.tv/t/eventsub-websockets-are-now-available-in-open-beta/41639), so changes could be made that may affect your application negatively until TESjs is able to update. You can try this out in TESjs `v1.0.0-beta.0` and higher. | ||
|
||
# Documentation | ||
Learn how to use TESjs by reading through the [documentation](/doc). Supplement your development with the Twitch EventSub [documentation](https://dev.twitch.tv/docs/eventsub) as well. | ||
|
@@ -21,11 +21,15 @@ TESjs is available for install through npm | |
```sh | ||
npm install tesjs | ||
``` | ||
Or in browsers through a CDN | ||
```html | ||
<script src="https://cdn.jsdelivr.net/gh/mitchwadair/[email protected]/dist/tes.min.js"></script> | ||
``` | ||
|
||
# Basic Usage | ||
Keep in mind that in order for your subscriptions to work, the url you are pointing to for the listener **MUST** use `HTTPS` and port `443`. More information can be found in the Twitch documentation [here](https://dev.twitch.tv/docs/eventsub). Their suggestion for testing locally is to use a product like [ngrok](https://ngrok.com/) to create an `HTTPS` endpoint to forward your local server (which is hosted on `HTTP`). | ||
Keep in mind that in order for your subscriptions to work when using `webhook` transport, the url you are pointing to for the listener **MUST** use `HTTPS` and port `443`. More information can be found in the Twitch documentation [here](https://dev.twitch.tv/docs/eventsub). Their suggestion for testing locally is to use a product like [ngrok](https://ngrok.com/) to create an `HTTPS` endpoint to forward your local server (which is hosted on `HTTP`). | ||
```js | ||
const TES = require('tesjs'); | ||
const TES = require("tesjs"); | ||
|
||
// initialize TESjs | ||
const tes = new TES({ | ||
|
@@ -34,40 +38,72 @@ const tes = new TES({ | |
secret: YOUR_CLIENT_SECRET //do not ship this in plaintext!! use environment variables so this does not get exposed | ||
}, | ||
listener: { | ||
type: "webhook", | ||
baseURL: "https://example.com", | ||
secret: WEBHOOKS_SECRET, | ||
} | ||
}); | ||
|
||
// define an event handler for the 'channel.update' event | ||
// define an event handler for the `channel.update` event | ||
// NOTES: | ||
// this handles ALL events of that type | ||
// events will not be fired until there is a subscription made for them | ||
tes.on('channel.update', event => { | ||
tes.on("channel.update", (event) => { | ||
console.log(`${event.broadcaster_user_name}'s new title is ${event.title}`); | ||
}); | ||
|
||
// create a new subscription for the 'channel.update' event for broadcaster '1337' | ||
tes.subscribe('channel.update', { | ||
broadcaster_user_id: '1337' | ||
}).then(_ => { | ||
console.log('Subscription successful'); | ||
}).catch(err => { | ||
console.log(err); | ||
}); | ||
// create a new subscription for the `channel.update` event for broadcaster "1337" | ||
tes.subscribe("channel.update", { broadcaster_user_id: "1337" }) | ||
.then(() => { | ||
console.log("Subscription successful"); | ||
}).catch(err => { | ||
console.log(err); | ||
}); | ||
``` | ||
|
||
# Browser | ||
TESjs supports WebSocket transport, and can be used in a browser environment | ||
```html | ||
<script src="https://cdn.jsdelivr.net/gh/mitchwadair/[email protected]/dist/tes.min.js"></script> | ||
<script> | ||
const config = { | ||
identity: { | ||
id: YOUR_CLIENT_ID, | ||
accessToken: YOUR_USER_ACCESS_TOKEN, | ||
}, | ||
listener: { type: "websocket" }, | ||
}; | ||
const tes = new TES(config); | ||
// define an event handler for the `channel.update` event | ||
// NOTES: | ||
// this handles ALL events of that type | ||
// events will not be fired until there is a subscription made for them | ||
tes.on("channel.update", (event) => { | ||
console.log(`${event.broadcaster_user_name}'s new title is ${event.title}`); | ||
}); | ||
// create a new subscription for the `channel.update` event for broadcaster "1337" | ||
tes.subscribe("channel.update", { broadcaster_user_id: "1337" }) | ||
.then(() => { | ||
console.log("Subscription successful"); | ||
}).catch(err => { | ||
console.log(err); | ||
}); | ||
</script> | ||
``` | ||
|
||
# Use an Existing Express Server | ||
TESjs uses Express under the hood to host a webhooks endpoint. If you already have a server running on Express that you want to use, you can pass it into the configuration object for TESjs. | ||
```js | ||
const TES = require('tesjs'); | ||
const express = require('express'); | ||
const TES = require("tesjs"); | ||
const express = require("express"); | ||
|
||
// create our Express server | ||
const app = express(); | ||
|
||
app.get('/', (req, res) => { | ||
res.send('OK'); | ||
app.get("/", (req, res) => { | ||
res.send("OK"); | ||
}); | ||
|
||
app.listen(8080); | ||
|
@@ -79,28 +115,28 @@ const tes = new TES({ | |
secret: YOUR_CLIENT_SECRET //do not ship this in plaintext!! use environment variables so this does not get exposed | ||
}, | ||
listener: { | ||
type: "webhook", | ||
baseURL: "https://example.com", | ||
secret: WEBHOOKS_SECRET, | ||
server: app | ||
} | ||
}); | ||
|
||
// define an event handler for the 'channel.update' event | ||
// define an event handler for the `channel.update` event | ||
// NOTES: | ||
// this handles ALL events of that type | ||
// events will not be fired until there is a subscription made for them | ||
tes.on('channel.update', event => { | ||
tes.on("channel.update", (event) => { | ||
console.log(`${event.broadcaster_user_name}'s new title is ${event.title}`); | ||
}); | ||
|
||
// create a new subscription for the 'channel.update' event for broadcaster '1337' | ||
tes.subscribe('channel.update', { | ||
broadcaster_user_id: '1337' | ||
}).then(_ => { | ||
console.log('Subscription successful'); | ||
}).catch(err => { | ||
console.log(err); | ||
}); | ||
// create a new subscription for the `channel.update` event for broadcaster "1337" | ||
tes.subscribe("channel.update", { broadcaster_user_id: "1337" }) | ||
.then(() => { | ||
console.log("Subscription successful"); | ||
}).catch(err => { | ||
console.log(err); | ||
}); | ||
``` | ||
|
||
# Problems/Suggestions/Questions? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ecf7145c2f81e267c50dcaa573a4511b19730bc17a036ace16e82317e571fc2ce638854f59fb4fe2c9ba979a0690548989524949f8c782cd0c62ef6ccb5fa95b |
Oops, something went wrong.