Giter VIP home page Giter VIP logo

eventsocket's Introduction

EventSocket

A Websocket Proxy and Management Tool for Twitch's EventSub Service

EventSub is a great method for receiving events from Twitch, but it does not currently support WebSockets, making it difficult for streamers who just want to receive events for themselves to their own machines without configuring servers.

EventSocket provides a WebSocket server that forwards events from EventSub webhooks to any WebSocket client, as well as providing a UI for enabling EventSub subscriptions.

How to Use

  1. Login to EventSocket
  2. Go to the settings page and generate an API key. Store it somewhere safe, since it cannot be accessed again after leaving the settings page.
  3. Using a WebSocket client, connect to wss://api.eventsocket.brendonovich.dev/socket?api_key=YOUR_API_KEY, replacing YOUR_API_KEY with the API key generated in step 2
  4. Enable some subscriptions in the Events tab on the website
  5. You should start receiving events through your socket!

Protocol

Most messages resemble those documented in the EventSub WebSockets RFC, with all messages having metadata and payload fields.

Event Notifications

Event notification messages are similar to EventSub events:

{
  "metadata": {
    "message_id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX",
    "message_timestamp": "2022-01-1T00:00:00Z",
    "message_type": "notification",
    "subscription_type": "channel.follow"
  },
  "payload": {
    // EventSub event data
    "event": {
      "broadcaster_user_id": "53168490",
      "broadcaster_user_login": "brendonovich",
      "broadcaster_user_name": "Brendonovich",
      "moderator_user_id": "53168490",
      "moderator_user_login": "brendonovich",
      "moderator_user_name": "Brendonovich",
      "user_id": "53168490",
      "user_login": "brendonovich",
      "user_name": "Brendonovich"
    },
    "subscription": {
      "type": "channel.follow"
    }
  }
}

Notice that there is no condition field in payload.subscription. Conditions are mostly used in EventSub to specify which user a subscription should receive events for, but since EventSocket already scopes the events you receive to your own user ID conditions are mostly redundant, and thus not used. This only affects the channel.raid subscription type, as from_broadcaster_id or to_broadcaster_id must be provided with EventSub. To account for this, EventSocket provides channel.raid.give and channel.raid.receive as subscription types.

channel.raid.give EventSub equivalent:

{
  "subscription": {
    "type": "channel.raid",
    "condition": {
      "from_broadcaster_id": "your user id"
    }
  }
}

channel.raid.receive EventSub equivalent:

{
  "subscription": {
    "type": "channel.raid",
    "condition": {
      "to_broadcaster_id": "your user id"
    }
  }
}

Keepalive

Every 15 seconds, the server will send a keepalive message. If this message is not received in a 15-20 second interval, it is safe to assume that the socket is disconnected and you should reconnect.

{
  "metadata": {
    "message_id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX",
    "message_timestamp": "2022-01-1T00:00:00Z",
    "message_type": "websocket_keepalive"
  },
  "payload": {} // No data
}

Reconnect

In the event that the EventSocket server you are connected to is about to shutdown (eg. during an update of the server), a reconnect message will be sent, after which you have 10 seconds to establish a new WebSocket connection before the old one is forcefully disconnected.

{
  "metadata": {
    "message_id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX",
    "message_timestamp": "2022-01-1T00:00:00Z",
    "message_type": "websocket_reconnect"
  },
  "payload": {} // No data
}

Self Hosting

If you don't like the idea of all your twitch events passing through my server, create an application in the Twitch Developer Console, and then pull and run the EventSocket API docker image with the following environment variables:

  • DATABASE_URL: Full address to a Postgres database
  • SECRET_KEY_BASE: A random string for use in encryption
  • TWITCH_CLIENT_ID: Client ID of your Twitch application
  • TWITCH_CLIENT_SECRET: Client Secret of your Twitch application
  • SELF_ORIGIN: IP or domain name you will use to access the API, eg: https://example.com
  • WEB_ORIGIN: IP or domain name you will use to access the frontend

To run the frontend, I would suggest forking this repo and using Vercel to host your own version. Add the full URL to where you are hosting the API as the VITE_API_URL environment variable in your Vercel project's settings. Everything else should already be configured, since https://app.eventsocket.brendonovich.dev is hosted on Vercel too.

eventsocket's People

Contributors

brendonovich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

erickwatson

eventsocket's Issues

Store information about users' sockets in distributed processes

Instead of storing each socket connection in the DB, it makes more sense to store it in memory. The idea is to have a Horde DynamicSupervisor that watches a NodeListener and a bunch of processes (one per user) that contain data about each user's connected sockets. These processes will be referenced by a Horde Registry that maps user ids to their respective process and will restart as needed by Horde on new machines when necessary, preserving their state.

Things to consider:

  • Does the 10 second grace period for websocket disconnects affect Horde's process distribution? I don't think so since the DynamicSupervisor will be a sibling of the endpoint, thus it should be able handle exit trapping on its own.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.