Class TwitchConduitSocketPool
java.lang.Object
com.github.twitch4j.eventsub.socket.conduit.TwitchConduitSocketPool
- All Implemented Interfaces:
IEventSubConduit
,AutoCloseable
Facilitates creating EventSub WebSocket shards for a new or existing Conduit.
Sample usage:
IEventSubConduit conduit = TwitchConduitSocketPool.create(spec -> {
spec.poolShards(4); // customizable pool size
spec.clientId("your-client-id");
spec.clientSecret("your-client-secret");
});
conduit.register(SubscriptionTypes.STREAM_ONLINE, b -> b.broadcasterUserId("71092938").build());
conduit.getEventManager().onEvent(StreamOnlineEvent.class, System.out::println);
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
static @NotNull TwitchConduitSocketPool
create
(@NotNull Consumer<ConduitSpec> spec) Creates a managed websocket pool to serve as shards for an EventSub Conduit.@NotNull String
@NotNull com.github.philippheuer.events4j.core.EventManager
long
int
int
register
(@NotNull EventSubSubscription subscription) Creates an eventsub subscription for this conduit.<C extends EventSubCondition,
B>
Optional<EventSubSubscription>register
(@NotNull SubscriptionType<C, B, ?> type, @NotNull Function<B, C> conditions) Creates an eventsub subscription for this conduit.boolean
unregister
(@NotNull EventSubSubscription subscription) Deletes an eventsub subscription from helix (and this conduit).
-
Method Details
-
register
Description copied from interface:IEventSubConduit
Creates an eventsub subscription for this conduit.- Specified by:
register
in interfaceIEventSubConduit
- Parameters:
subscription
- the eventsub subscription to be registered- Returns:
- the created subscription
-
register
public <C extends EventSubCondition,B> Optional<EventSubSubscription> register(@NotNull @NotNull SubscriptionType<C, B, ?> type, @NotNull @NotNull Function<B, C> conditions) Description copied from interface:IEventSubConduit
Creates an eventsub subscription for this conduit.- Specified by:
register
in interfaceIEventSubConduit
- Type Parameters:
C
- the condition type associated with the subscriptionB
- the builder type associated with the condition- Parameters:
type
- the type of EventSub subscription to be created; seeSubscriptionTypes
conditions
- the conditions associated with the eventsub subscription- Returns:
- the created subscription, or empty if the subscription could not be registered.
-
unregister
Description copied from interface:IEventSubConduit
Deletes an eventsub subscription from helix (and this conduit).- Specified by:
unregister
in interfaceIEventSubConduit
- Parameters:
subscription
- the eventsub subscription to be destroyed- Returns:
- whether the specified subscription was successfully deleted
-
getLatency
public long getLatency()- Specified by:
getLatency
in interfaceIEventSubConduit
- Returns:
- the average latency for the socket shards in milliseconds, or -1 if unknown
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-
getManagedShardCount
public int getManagedShardCount() -
create
@NotNull public static @NotNull TwitchConduitSocketPool create(@NotNull @NotNull Consumer<ConduitSpec> spec) throws CreateConduitException, ConduitNotFoundException, ConduitResizeException, ShardTimeoutException, ShardRegistrationException Creates a managed websocket pool to serve as shards for an EventSub Conduit.This pool can be used to create a completely new conduit, or augment an existing conduit.
Note: Twitch limits each Client ID to five (5) enabled conduits, with up to 20,000 shards per conduit. Please do not try to create 20,000 websockets from a single server.
- Parameters:
spec
- the specification by which to create a websocket pool for an EventSub Conduit- Returns:
- a
TwitchConduitSocketPool
instance - Throws:
NullPointerException
- if the passed spec isnull
IllegalArgumentException
- if the specified conduit configuration is invalidCreateConduitException
- ifTwitchHelix.createConduit(String, int)
failsConduitNotFoundException
- if a specific Conduit ID was specified, but not found withinTwitchHelix.getConduits(String)
when checking the shard countConduitResizeException
- ifTwitchHelix.updateConduit(String, String, int)
fails while resizing the Conduit to accommodate the number of shards in the specShardTimeoutException
- if any of the underlying sockets for this pool do not receive session_welcome from Twitch before the timeout in the spec (default: 15 seconds)ShardRegistrationException
- ifTwitchHelix.updateConduitShards(String, ShardsInput)
fails while registering the created sockets with the Conduit ID- See Also:
-
getEventManager
@NotNull public @NotNull com.github.philippheuer.events4j.core.EventManager getEventManager()- Specified by:
getEventManager
in interfaceIEventSubConduit
- Returns:
- the event manager for eventsub notifications
-
getShardOffset
public int getShardOffset() -
getConduitId
- Specified by:
getConduitId
in interfaceIEventSubConduit
- Returns:
- the ID associated with this Conduit
-