Categorygithub.com/seventv/eventapi
module
0.0.0-20240222185007-f2b9f5fa3600
Repository: https://github.com/seventv/eventapi.git
Documentation: pkg.go.dev

# README

THIS REPO IS ARCHIVED PLEASE SEE

https://github.com/seventv/seventv

for more info

7TV EventAPI

The Event API allows developers to receive real-time updates on 7TV data and implement instantaneous feedback in applications. This service is used primarily to push channel emote changes to clients without requiring a manual refresh and it powers systems such as personal emotes. To get started on using this service see the documentation below.

Supported Protocols

  • Server-Sent Events / HTTP
  • WebSocket
  • Webhooks (planned)

API Documentation

Versions

Base URL: https://events.7tv.io

NameStatus
V3✅ Supported
V2Skipped
V1⚠️ Deprecated

Opcodes

Operation codes are used to identify what kind of payload an event contains.

OpNameTypeDescription
0Dispatch⬇️A standard event message, sent when a subscribed event is emitted
1Hello⬇️Received upon connecting, presents info about the session
2Heartbeat⬇️Ensures the connection is still alive
4Reconnect⬇️Server wants the client to reconnect
5Ack⬇️Server acknowledges an action by the client
6Error⬇️An error occured, you should log this
7End of Stream⬇️The server will send no further data and imminently end the connection
33Identify⬆️Authenticate with an account
34Resume⬆️Try to resume a previous session
35Subscribe⬆️Watch for changes on specific objects or sources. Don't smash it!
36Unsubscribe⬆️Stop listening for changes
37Signal⬆️

Legends: ⬆️ sent by client, ⬇️ sent by server

Close codes

Close codes provide a reason for the closure of a connection to help clients understand what happened.

CodeNameDescriptionReconnect?
4000Server Erroran error occured on the server's endYes
4001Unknown Operationthe client sent an unexpected opcodeNo¹
4002Invalid Payloadthe client sent a payload that couldn't be decodedNo¹
4003Auth Failurethe client unsucessfully tried to identifyNo¹
4004Already Identifiedthe client wanted to identify againNo¹
4005Rate Limitedthe client is being rate-limitedMaybe³
4006Restartthe server is restarting and the client should reconnectYes
4007Maintenancethe server is in maintenance mode and not accepting connectionsYes²
4008Timeoutthe client was idle for too longYes
4009Already Subscribedthe client tried to subscribe to an event twiceNo¹
4010Not Subscribedthe client tried to unsubscribe from an event they weren't subscribing toNo¹
4011Insufficient Privilegethe client did something that they did not have permission forMaybe³

¹ this code indicate a bad client implementation. you must log such error and fix the issue before reconnecting ² reconnect with significantly greater delay, i.e at least 5 minutes, including jitter ³ only reconnect if this was initiated by action of the end-user

Payloads

Dispatch (0)

KeyTypeDescription
typeEventTypethe event type
bodyChangeMapdetailed changes
ChangeMap
KeyTypeDescription
idObjectIDthe object's ID
kindint8the object kind
contextual?boolif true, this event represents a change local only to the current session
actorUserthe user responsible for these changes
added?[]ChangeFielda list of added fields
updated?[]ChangeFielda list of updated fields
removed?[]ChangeFielda list of removed fields
pushed?[]ChangeFielda list of items pushed to an array
pulled?[]ChangeFielda list of items pulled from an array
ChangeField
KeyTypeDescription
keystringthe key in context
indexint?if the field is an array, this is the index of the item within the array that was updated
nested¹boolif true, this means the current value is nested deeper
old_valueobject or nilthe previous value
valueobject, []ChangeField or nilthe new value

¹ the value field will always be []ChangeField when nested is true.

Hello (1)

KeyTypeDescription
heartbeat_intervaluint32interval in milliseconds between each heartbeat
session_idstringunique token for this session, used for resuming and mutating the session
subscription_limitint32the maximum amount of subscriptions this connection can initiate

Heartbeat

KeyTypeDescription
countuint64The amount of heartbeats so far

Ack (5)

KeyTypeDescription
commandstringthe acknowledged sent opcode in text form
dataechothe data sent by the client

Resume (34)

KeyTypeDescription
session_idstringthe id of the previous session

Subscribe (35)

KeyTypeDescription
typestringsubscription type
conditionstring mapfilter messages by conditions

Example

{
    "op": 35,
    "d": {
        "type": "emote_set.update",
        "condition": {
            // valid fields in the condition depend on the subscription type
            // though in most cases except creations, object_id is acceptable
            // to filter for a specific object.

            "object_id": "62cdd34e72a832540de95857"
        }
    }
}

Unsubscribe (36)

KeyTypeDescription
typestringsubscription type
condition?string mapfilter messages by conditions
{
  "op": 36, 
  "d": {
    "type": "emote_set.update",
    "condition": {
      "object_id": "62cdd34e72a832540de95857"
    }
  }
}

End of Stream (7)

End of Stream events are sent when the connection is closed by the server.

The close code provided in the event indicates the reason for the disconnect and whether or not the client should reconnect.

KeyTypeDescription
codeuint16close code
messagestringa text message about the closure

Subscription types

Subscription Types
TypeKind
System - Announcementsystem.announcement
Create Emoteemote.create
Update Emoteemote.update
Delete Emoteemote.delete
Create Emote Setemote_set.create
Update Emote Setemote_set.update
Delete Emote Setemote_set.delete
Create Useruser.create
Update Useruser.update
Delete Useruser.delete
Add User Connectionuser.add_connection
Update User Connectionuser.update_connection
Delete User Connectionuser.delete_connection
Create Cosmeticcosmetic.create
Update Cosmeticcosmetic.update
Delete Cosmeticcosmetic.delete
Create Entitlemententitlement.create
Update Entitlemententitlement.update
Delete Entitlemententitlement.delete

If you'd like to receive all events about an object, it is also possible to use an asterisk symbol as a wildcard. For example, using the type emote.* will subscribe to each of emote.create, emote.update and emote.delete.


EventStream / Server-Sent Events (HTTP)

EventStream, or Server-Sent Events (SSE) is the recommended way to use the EventAPI. This is a one-way stream which operates via a regular HTTP connection rather than a different protocol such as WebSocket.

The EventStream access point is https://events.7tv.io/v3. The following sections will cover how to initiate and modify the connection.

Message Structure (EventStream)

In a browser environment, the EventSource interface exists to automatically handle an EventStream endpoint.

Otherwise, to read and parse this data refer to the spec for Server-Sent Events.

Opcodes are sent in their text form as the event's type, and the data will always be JSON.

Connection (EventStream)

Upon opening the connection, you will receive a [1] HELLO event.

Inline Event Subscriptions (EventStream)

It is possible to add subscriptions directly in the connection string, by appending a @ to the URL followed by a URL-encoded string with the following syntax,

{type}<{condition1}={value1};{condition2}={value2}>;...

where type is a subscription type, then wrapped between brackets (<...>) are a list of conditions. Separate each subscription by a comma. The same type can be subscribed to multiple times with different conditions.

The entire inline subscription string must be URL-encoded.

Full examples

Subscribe to updates on a specific emote set
: GET https://events.7tv.io/v3@emote_set.update<object_id=62cdd34e72a832540de95857>

Subscribe to all entitlement and cosmetic events on a specific channel
: GET https://events.7tv.io/v3@entitlement.*<host_id=60867b015e01df61570ab900;connection_id=1234>,cosmetic.*<host_id=60867b015e01df61570ab900;connection_id=1234>

At this time, it is possible to subscribe with no conditions and effectively turn the connection into a Firehose, however this may be restricted in the future.

Managing subscriptions (EventStream)

Adding or removing a subscription is done by sending a request via a REST endpoint and passing your session ID to authenticate.

The endpoint for managing subscriptions is currently unavailable. You may use Inline Event Subscriptions in the meantime.

Acks (EventStream)

An ack will be sent when the session is mutated, such as when subscribing or unsubscribing. ACKs are also sent when using Inline Event Subscriptions immediately after the session becomes ready. This can be used to confirm the validity of the inline subscription string.

Handling ACKs is optional, but allows an implementation to confirm that the server accepted their input.

Dispatches (EventStream)

Once subscriptions are active, you will receive [0] DISPATCH events


WebSocket

For apps which must issue commands frequently, WebSocket may be a better choice.

The websocket access point is wss://events.7tv.io/v3. The following sections will cover how to maintain and modify the connection.

Message Structure (WebSocket)

WebSocket messages are sent in JSON format and are composed of three root properties, op is the opcode, t is the timestamp of the message's formation, and d is the data payload.

KeyTypeDescription
opuint8message operation code
tdatetimestamp of the message's formation in unix millis
dobjectgeneric data payload

Connection (WebSocket)

Upon establishing a connection, you will receive a [1] HELLO event.

Heartbeat (WebSocket)

The server will send periodic heartbeats at the interval specified in the Hello payload. If heartbeats are missed for 3 cycles, the connection can be considered dead (i.e due to an error or network issue) and you should reconnect.

Resuming (WebSocket)

When a connection is dropped with a non-normal, non-error closure, it is possible to resume the session to restore subscriptions and replay missed events.

To initiate a resume, send the opcode [34] RESUME and pass the session ID from the previous connection. If successful, the server will acknowledge the resume with an [5] ACK. Previous subscriptions will be restored, and missed [0] DISPATCH events will replay in sequence.

Managing subscriptions (WebSocket)

A subscription consists of a type and a condition. This is where you can choose exactly what kind of data your application needs.

Valid subscription types can be found here.

Subscribing (WebSocket)

To set up a subscription, you may send the opcode [35] SUBSCRIBE and these fields

You are allowed to subscribe multiple times to the same type, however, duplicated conditions will result in a disconnect with code 4009 Already Subscribed.

Unsubscribing (WebSocket)

When an event source is no longer needed, you should unsubscribe from it to avoid receiving unnecessary data with [36] UNSUBSCRIBE.

It is also possible to unsubscribe from an entire event type at once by leaving the condition field empty.

Acks (WebSocket)

An ack will be sent when a command is successfully executed, such as subscribing or unsubscribing. Handling ACKs is optional, but allows an implementation to confirm that the server accepted their input.

Dispatches (WebSocket)

Once subscriptions are active, you may start receiving messages with the [0] DISPATCH opcode. These events notify of changes to an object and allow you to keep your state up to date.


View legacy v1 documentation

# Packages

No description provided by the author