Skip to content

Commit

Permalink
Merge pull request #64 from mitchwadair/ws-dev
Browse files Browse the repository at this point in the history
#33: Add WebSocket Transport Support (beta)
  • Loading branch information
mitchwadair authored Nov 13, 2022
2 parents 05fcb0f + f624d71 commit 35529ab
Show file tree
Hide file tree
Showing 28 changed files with 1,365 additions and 391 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -51,7 +51,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -65,4 +65,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
27 changes: 11 additions & 16 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

20 changes: 15 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,33 @@ on:

jobs:
test:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x]
node-version: [12, 14, 16]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install Homebrew
id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master
- run: brew install twitchdev/twitch/twitch-cli
- run: npm ci
- run: npm run build --if-present
- run: npm test

build-check:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm ci
- run: npm run build
- run: bash scripts/verify-checksum.sh
103 changes: 2 additions & 101 deletions .gitignore
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
4 changes: 3 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
package.json
package.json
*.md
*.yml
92 changes: 64 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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({
Expand All @@ -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);
Expand All @@ -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?
Expand Down
1 change: 1 addition & 0 deletions checksum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ecf7145c2f81e267c50dcaa573a4511b19730bc17a036ace16e82317e571fc2ce638854f59fb4fe2c9ba979a0690548989524949f8c782cd0c62ef6ccb5fa95b
Loading

0 comments on commit 35529ab

Please sign in to comment.