TwitchEventListener Node
The TwitchEventListener provides a convenient way to react to a specific type of Twitch EventSub event without needing complex filtering logic in your main scripts. It acts as a dedicated listener for a single event subscription.
Overview
Think of TwitchEventListener as a signal relay or filter. You configure it with:
- An existing
TwitchEventsubnode instance. - The specific
TwitchEventsubDefinition.Typeyou are interested in (e.g.,CHANNEL_FOLLOW,CHANNEL_SUBSCRIBE).
The listener then connects to the general event signal of the provided TwitchEventsub node. When any event comes from TwitchEventsub, this listener checks if the event's type matches the one it's configured for. If it matches, the TwitchEventListener emits its own received signal, passing along the raw data dictionary for that specific event.
Auto-subscribe: When the Subscription type is selected, the Inspector shows the condition fields that subscription needs (the same UI used by TwitchEventsubConfig). Filling them in lets TwitchEventListener subscribe to Twitch on its own during _ready(), whenever the linked TwitchEventsub doesn't already have a matching subscription — useful for events you only listen to in one place, without setting up a separate TwitchEventsubConfig. If a subscription for that type already exists (with any conditions) on the TwitchEventsub node, that existing one is used instead and nothing new is created. Leave condition empty (or set Ensure Subscription On Ready to false) for a purely listen-only node whose subscription is configured entirely on the TwitchEventsub node.
C# note
In C#, TwitchEventListener is a generic class: TwitchEventListener<T>, where T is the specific event type (e.g. TwitchChannelFollowEvent, TwitchChannelChatMessageEvent). This means the received(data: Dictionary) signal you'd get in GDScript becomes a strongly-typed Received event carrying a T object instead of a raw Dictionary, so data["user_name"] becomes e.UserName with compile-time checking and autocomplete. Every EventSub subscription type has a matching event class generated under TwitcherSharp.EventSub.Generated.*.
Prerequisites
- Add the Node: Add a
TwitchEventListenernode to your scene. It's often useful as a child of the node that needs to react to the specific event. - EventSub Dependency: Assign a configured
TwitchEventsubinstance to theEventsubproperty in the Inspector. (Autobinding will use the first Eventsub found in the scene) - Conditions: Either fill in the
Conditionfields shown for the selectedSubscriptionso the listener can subscribe itself, or make sure theTwitchEventsubnode assigned in step 2 is already subscribed to the event type you intend to listen for.
Configuration (Inspector Properties)
Eventsub(TwitchEventsub): Required. The instance ofTwitchEventsubthat this listener will connect to and receive events from. Automatically assigned if only one instance ofTwitchEventsubexists.Subscription(TwitchEventsubDefinition.Type): Required. Select the specific type of EventSub event this listener should react to from the enum list (e.g.,CHANNEL_FOLLOW,CHANNEL_SUBSCRIBE,CHANNEL_CHEER,CHANNEL_CHANNEL_POINTS_CUSTOM_REWARD_REDEMPTION, etc.). A "To dev.twitch.tv" button next to it links straight to Twitch's documentation for the selected type.Condition(Dictionary): The condition values (e.g.broadcaster_user_id,moderator_user_id) required by the selectedSubscription, shown as one input field per condition. Only used to subscribe when theTwitchEventsubdoesn't already have a matching subscription — leave it empty to rely entirely on a subscription configured elsewhere.Ensure Subscription On Ready(bool): Defaulttrue. When enabled, the listener callsensure_subscription()in_ready()to subscribe itself if needed. Set tofalseif you want to manage subscribing manually (e.g. to callensure_subscription()later, once the required user IDs are known).
C# note
In C#, Subscription becomes SubscriptionDefinition (a TwitchEventSubDefinition, e.g. TwitchEventSubDefinition.ChannelFollow) and can be set either in the editor on a TwitchEventListener node, or directly from code before adding the node to the scene tree; see the usage example below.
Signals
received(data: Dictionary)- Emitted when the linked
TwitchEventsubnode receives an event whose type exactly matches theSubscriptiontype configured for this listener. data: ADictionarycontaining the raw payload of the received EventSub event, exactly as provided by Twitch. The structure of this dictionary depends entirely on the specific event type. Refer to the official Twitch EventSub documentation for the payload details of each event.
- Emitted when the linked
C# note
TwitchEventListener<T>.Received is a event Action<T>: subscribe with listener.Received += OnReceived; or listener.ConnectReceived(...) / .DisconnectReceived(...).
Methods
start_listening() -> void- Manually connects the listener's internal handler to the
eventsignal of the assignedTwitchEventsubnode. This is usually called automatically when the node enters the scene tree (_enter_tree). You might call this if you previously calledstop_listening()and want to resume.
- Manually connects the listener's internal handler to the
stop_listening() -> void- Manually disconnects the listener's internal handler from the
eventsignal of the assignedTwitchEventsubnode. This prevents the listener from processing further events untilstart_listening()is called again. This is usually called automatically when the node exits the scene tree (_exit_tree).
- Manually disconnects the listener's internal handler from the
ensure_subscription() -> void- Makes sure the
TwitchEventsubhas a subscription this listener can receive events from: it looks for an existing subscription of the selectedSubscriptiontype first, and if none is found, subscribes on its own usingCondition(if fully filled in). Called automatically in_ready()whenEnsure Subscription On Readyistrue. Logs an error viaTwitchLoggerif no matching subscription exists andConditionisn't complete enough to create one, or if Twitch rejects the subscription (seeTwitchEventsub'ssubscription_failedsignal).
- Makes sure the
get_conditions() -> Dictionary- Returns the
Conditionvalues that would be used to subscribe, resolving any user picked via the Inspector's user-search field to their user ID.
- Returns the
get_missing_conditions() -> Array[StringName]- Returns the condition keys required by the selected
Subscriptionthat don't have a value yet.
- Returns the condition keys required by the selected
C# note
Same in C#: listener.StartListening() / listener.StopListening().
Usage Example
Imagine you want a specific Node (MyFollowHandler) to react only to follow events, and have it subscribe itself rather than configuring channel.follow separately on the TwitchEventsub node.
- Add a
TwitchEventListenernode (e.g., as a child ofMyFollowHandler). - In the Inspector for the
TwitchEventListener:- Assign your main
TwitchEventsubnode to theEventsubproperty. - Select
CHANNEL_FOLLOWfor theSubscriptionproperty. - Fill in the
broadcaster_user_id(andmoderator_user_id) fields that appear underCondition— e.g. using the user-search field, or leaveConditionempty ifchannel.followis already subscribed elsewhere.
- Assign your main
# Script attached to MyFollowHandler node
# Reference the TwitchEventListener node (assuming it's a child)
@onready var follow_listener: TwitchEventListener = $FollowEventListener
func _ready():
follow_listener.received.connect(_on_follow_event_received)
# This function will ONLY be called when a CHANNEL_FOLLOW event occurs
func _on_follow_event_received(data: Dictionary):
# Process the follow event data
# The structure of 'data' depends on the Twitch EventSub specification
# for channel.follow events.
var follower_name = data["user_name"]
print("Received a new follow from: %s!" % follower_name)
# Trigger game logic, update UI, etc.// Script attached to MyFollowHandler node
using Godot;
using TwitcherSharp.EventSub;
using TwitcherSharp.EventSub.Generated.ChannelFollow;
using TwitcherSharp.Extensions;
public partial class MyFollowHandler : Node
{
// Reference the TwitchEventListener node (assuming it's a child)
private TwitchEventListener<TwitchChannelFollowEvent> _followListener;
public override void _Ready()
{
_followListener = this.GetTwitcherNode<TwitchEventListener<TwitchChannelFollowEvent>>("FollowEventListener");
_followListener.Received += OnFollowEventReceived;
}
// This will ONLY be called when a CHANNEL_FOLLOW event occurs
private void OnFollowEventReceived(TwitchChannelFollowEvent e)
{
// 'e' is already a strongly-typed TwitchChannelFollowEvent, no dictionary lookups needed
GD.Print($"Received a new follow from: {e.UserName}!");
// Trigger game logic, update UI, etc.
}
}C# note
The example above binds to a TwitchEventListener node already configured in the editor via FromObject. If you'd rather build it entirely from code instead of placing the node in the editor:
var listener = new TwitchEventListener<TwitchChannelFollowEvent>
{
SubscriptionDefinition = TwitchEventSubDefinition.ChannelFollow
};
AddChild(listener.ToGodotObject() as Node);
listener.Received += OnFollowEventReceived;