Here you will find a simple Express web app illustrating how to authenticate Twitch API calls using the OAuth authorization code flow. This sample uses Express, Passport for the server and ReactJS for the client.
DEMO: Heroku App
- After you have cloned this repository, use npm to install dependencies on the backend.
$ npm install
cd
to the client folder
$ cd client
- Use
yarn
to install dependencies
$ yarn add
Before running this sample, you will need to set siz configuration fields in your .env:
- TWITCH_CLIENT_ID - This is the Client ID of your registered application. You can register a new application at [https://dev.twitch.tv/dashboard/apps]
- TWITCH_CLIENT_SECRET - This is the secret generated for you when you register your application, do not share this. In a production environment, it is STRONGLY recommended that you do not store application secrets on your file system or in your source code.
- SESSION_SECRET - This is the secret Express session middleware uses to sign the session ID cookie.
- CALLBACK_URL - This is the callback URL you supply when you register your application. To run this sample locally use [http://localhost:3000/auth/twitch/callback]
- TWITCH_WEBHOOK_BASE_URL - Base URL of where you want the Twitch webhooks to be received.
- PORT - Port where you want to run your server.
Optionally, you may modify the requested scopes when the /auth/twitch route is defined.
After setting these fields, you may run the server.
$ node index.js
Before running the client, you need to set the two configuration fields in the .env
file.
- REACT_APP_TWITCH_CLIENT_ID - Your Twitch Client ID
- REACT_APP_SERVER_BASE_URL - Base URL for your server. To test locally you may use [http://localhost:4000]
- REACT_APP_SERVER_BASE_DOMAIN - Domain name of your server. For local you may user
localhost
After setting these fields, you may run the server
$ npm start
- One embedded livestream of the selected streamer and the chat for that livestream is shown on the page.
- Events shows two types of events - Follows and stream events.
- Twitch API for searching
channels
does a fuzzy search on the user name. So it might return a stream of some other user with similar name. This is more of a caveat.
reactjs
client makes a OAuth request to theexpress
server.- User is redirected to the Twitch login.
- Once user logs in to Twitch, the session is stored on the server using
Passport
. A JWT token is generated and sent to the client. - Once the user chooses his/her favorite streamer, a request is sent to the server to subscribe to webhooks for follow and stream change events.
- A socket connection is established between the client and the server when the user access
/stream
route. - As soon as a webhook is received for a subscribed evernt - a socket event is emitted.
- How is the appropriate client selected for sending the socket event? - When the socket connection is initially established, the user is authorized using a JWT token. Server decodes this token and extracts the
user_id
from it. The server maintains a map of all the users and their corresponding sockets using thisuser_id
. - How are different webhook subscriptions maintained for different users? - Each webhook subscription for a user contains the
user_id
in the URL. The webhook callback looks something like this -/webhook/callback/:user_id
. Whenever we receive a webhook event, we figure out the appropriate user from the URL. Once we have theuser_id
, we get the socket for this user from our map of active clients and emit on that socket.
AWS provides all the services that we need to run this app. We may use
- AWS Lambda to host our controller functions. These will be functions that perform the business logic of our app.
- AWS APIGateway to host our routes. These routes will call our Lambda functions.
- AWS Amplify to host our static client content.
The advantage of using all AWS services is that scaling will be automatically taken care of. AWS will dynamically scale resources as demand rises or falls.
AWS APIGateway also has EDGE-Aware services which route the client calls to the nearest CDN.
The bottleneck would be the webhook receiver. We have only one route which receives these webhooks and fires events to the clients. With an increase in clients, we will start seeing delays in the event delivery.