Class TwitchChatConnectionPool

java.lang.Object
com.github.twitch4j.common.pool.AbstractConnectionPool<C>
com.github.twitch4j.common.pool.SubscriptionConnectionPool<C,​X,​Y,​Z>
com.github.twitch4j.common.pool.TwitchModuleConnectionPool<TwitchChat,​java.lang.String,​java.lang.String,​java.lang.Boolean,​TwitchChatBuilder>
com.github.twitch4j.chat.TwitchChatConnectionPool
All Implemented Interfaces:
ITwitchChat, com.github.twitch4j.common.pool.TransactionalSubscriber<java.lang.String,​java.lang.String,​java.lang.Boolean>, java.lang.AutoCloseable

public class TwitchChatConnectionPool
extends com.github.twitch4j.common.pool.TwitchModuleConnectionPool<TwitchChat,​java.lang.String,​java.lang.String,​java.lang.Boolean,​TwitchChatBuilder>
implements ITwitchChat
A pool for TwitchChat connections.

This pool is easiest to use for:

  • Reading from many channels without an account
  • Reading from many channels with an account
  • Reading from many channels using valid proxies
  • Reading and sending messages/whispers with only one account
Other applications are possible, but harder to configure. See below for a list of warnings.

Warning: If a custom ScheduledThreadPoolExecutor is specified, its corePoolSize must be large enough for the threads required by connections made by this class.

Warning: If a chatAccount is to be shared across multiple connections and used to send messages, one should use advancedConfiguration to ensure the two are using a shared Bucket.

Note: If whispers are to be sent using this pool, one must manually join the channel to send the whisper from first. If chatAccount's are dynamically supplied such that no two connections are using the same account, one can set twitchChatBuilder.withAutoJoinOwnChannel(true) via advancedConfiguration to avoid the manual join.

  • Nested Class Summary

    Nested Classes
    Modifier and Type Class Description
    static class  TwitchChatConnectionPool.TwitchChatConnectionPoolBuilder<C extends TwitchChatConnectionPool,​B extends TwitchChatConnectionPool.TwitchChatConnectionPoolBuilder<C,​B>>  

    Nested classes/interfaces inherited from class com.github.twitch4j.common.pool.TwitchModuleConnectionPool

    com.github.twitch4j.common.pool.TwitchModuleConnectionPool.TwitchModuleConnectionPoolBuilder<C extends java.lang.Object,​X extends java.lang.Object,​Y extends java.lang.Object,​Z extends java.lang.Object,​B extends java.lang.Object,​C2 extends com.github.twitch4j.common.pool.TwitchModuleConnectionPool<C,​X,​Y,​Z,​B>,​B2 extends com.github.twitch4j.common.pool.TwitchModuleConnectionPool.TwitchModuleConnectionPoolBuilder<C,​X,​Y,​Z,​B,​C2,​B2>>

    Nested classes/interfaces inherited from class com.github.twitch4j.common.pool.SubscriptionConnectionPool

    com.github.twitch4j.common.pool.SubscriptionConnectionPool.SubscriptionConnectionPoolBuilder<C extends java.lang.Object,​S extends java.lang.Object,​T extends java.lang.Object,​U extends java.lang.Object,​C2 extends com.github.twitch4j.common.pool.SubscriptionConnectionPool<C,​S,​T,​U>,​B extends com.github.twitch4j.common.pool.SubscriptionConnectionPool.SubscriptionConnectionPoolBuilder<C,​S,​T,​U,​C2,​B>>

    Nested classes/interfaces inherited from class com.github.twitch4j.common.pool.AbstractConnectionPool

    com.github.twitch4j.common.pool.AbstractConnectionPool.AbstractConnectionPoolBuilder<C extends java.lang.Object,​C2 extends com.github.twitch4j.common.pool.AbstractConnectionPool<C>,​B extends com.github.twitch4j.common.pool.AbstractConnectionPool.AbstractConnectionPoolBuilder<C,​C2,​B>>
  • Field Summary

    Fields
    Modifier and Type Field Description
    protected boolean automaticallyPartOnBan
    Whether chat connections should automatically part from channels they have been banned from.
    protected @NonNull java.util.function.Supplier<com.github.philippheuer.credentialmanager.domain.OAuth2Credential> chatAccount
    Provides a chat account to be used when constructing a new TwitchChat instance.

    Fields inherited from class com.github.twitch4j.common.pool.TwitchModuleConnectionPool

    advancedConfiguration, executor, proxyConfig

    Fields inherited from class com.github.twitch4j.common.pool.SubscriptionConnectionPool

    disposeUnusedConnections, maxSubscriptionsPerConnection, saturatedConnections, subscriptions, unsaturatedConnections
  • Constructor Summary

    Constructors
    Modifier Constructor Description
    protected TwitchChatConnectionPool​(TwitchChatConnectionPool.TwitchChatConnectionPoolBuilder<?,​?> b)  
  • Method Summary

    Modifier and Type Method Description
    static TwitchChatConnectionPool.TwitchChatConnectionPoolBuilder<?,​?> builder()  
    protected TwitchChat createConnection()  
    protected void disposeConnection​(TwitchChat connection)  
    java.util.Map<java.lang.String,​java.lang.String> getChannelIdToChannelName()
    Note: this map does not dynamically update unlike TwitchChat.getChannelIdToChannelName()
    java.util.Map<java.lang.String,​java.lang.String> getChannelNameToChannelId()
    Note: this map does not dynamically update unlike TwitchChat.getChannelNameToChannelId()
    java.util.Set<java.lang.String> getChannels()
    Returns a set of all currently joined channels (without # prefix)
    protected java.lang.String getRequestFromSubscription​(java.lang.String s)  
    protected int getSubscriptionSize​(java.lang.String s)  
    protected java.lang.String handleDuplicateSubscription​(TwitchChat twitchChat, TwitchChat old, java.lang.String s)  
    protected java.lang.String handleSubscription​(TwitchChat twitchChat, java.lang.String s)  
    protected java.lang.Boolean handleUnsubscription​(TwitchChat twitchChat, java.lang.String s)  
    boolean isChannelJoined​(java.lang.String channelName)
    Check if Chat is currently in a channel
    void joinChannel​(java.lang.String channelName)
    Joins a channel
    boolean leaveChannel​(java.lang.String channelName)
    Parts from a channel
    boolean sendMessage​(java.lang.String channel, java.lang.String message, @Nullable java.util.Map<java.lang.String,​java.lang.Object> tags)
    Sends a message to the channel while including the specified message tags.
    boolean sendMessage​(java.lang.String channelToIdentifyChatInstance, java.lang.String targetChannel, java.lang.String message)
    Sends a message from the TwitchChat identified either to a channel or directly on the socket.
    boolean sendMessage​(java.lang.String channelToIdentifyChatInstance, java.lang.String targetChannel, java.lang.String message, @Nullable java.util.Map<java.lang.String,​java.lang.Object> tags)
    Sends a message from the identified TwitchChat instance with the specified tags.
    boolean sendMessage​(java.lang.String channelToIdentifyChatInstance, java.lang.String targetChannel, java.lang.String message, java.lang.String nonce, java.lang.String replyMsgId)
    Sends a message from the identified TwitchChat instance with an optional nonce or reply parent.
    boolean sendPrivateMessage​(java.lang.String channelToIdentifyChatInstance, java.lang.String toChannel, java.lang.String message)
    Sends a whisper.
    java.lang.String subscribe​(java.lang.String s)
    Joins a channel.
    java.lang.Boolean unsubscribe​(java.lang.String s)
    Parts from a channel.

    Methods inherited from class com.github.twitch4j.common.pool.TwitchModuleConnectionPool

    createEventManager, getConnectionEventManager, getDefaultConnectionEventManager, getEventManager, getExecutor

    Methods inherited from class com.github.twitch4j.common.pool.SubscriptionConnectionPool

    getConnections, numConnections, numSubscriptions

    Methods inherited from class com.github.twitch4j.common.pool.AbstractConnectionPool

    close

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • chatAccount

      @NonNull protected final @NonNull java.util.function.Supplier<com.github.philippheuer.credentialmanager.domain.OAuth2Credential> chatAccount
      Provides a chat account to be used when constructing a new TwitchChat instance. By default, this yields null, which corresponds to an anonymous connection.
    • automaticallyPartOnBan

      protected final boolean automaticallyPartOnBan
      Whether chat connections should automatically part from channels they have been banned from. This is useful for reclaiming subscription headroom so a minimal number of chat instances are running. By default false so that a chat instance can (eventually) reconnect if a unban occurs.
  • Constructor Details

  • Method Details

    • sendMessage

      public boolean sendMessage​(java.lang.String channel, java.lang.String message, @Unofficial @Nullable @Nullable java.util.Map<java.lang.String,​java.lang.Object> tags)
      Description copied from interface: ITwitchChat
      Sends a message to the channel while including the specified message tags.
      Specified by:
      sendMessage in interface ITwitchChat
      Parameters:
      channel - the name of the channel to send the message to.
      message - the message to be sent.
      tags - the message tags (unofficial).
      Returns:
      whether the message was added to the queue
    • sendMessage

      public boolean sendMessage​(java.lang.String channelToIdentifyChatInstance, java.lang.String targetChannel, java.lang.String message)
      Sends a message from the TwitchChat identified either to a channel or directly on the socket.
      Parameters:
      channelToIdentifyChatInstance - the channel used to identify which TwitchChat instance should be used to send the message; the instance must be subscribed to this channel.
      targetChannel - the channel to send the message to, if not null (otherwise it is sent directly on the socket).
      message - the message to be sent.
      Returns:
      whether a TwitchChat instance was found and used to send the message
    • sendMessage

      @Unofficial public boolean sendMessage​(java.lang.String channelToIdentifyChatInstance, java.lang.String targetChannel, java.lang.String message, java.lang.String nonce, java.lang.String replyMsgId)
      Sends a message from the identified TwitchChat instance with an optional nonce or reply parent.
      Parameters:
      channelToIdentifyChatInstance - the channel used to identify which TwitchChat instance should be used to send the message; the instance must be subscribed to this channel.
      targetChannel - the channel to send the message to, if not null (otherwise it is sent directly on the socket).
      message - the message to be sent.
      nonce - the cryptographic nonce (optional).
      replyMsgId - the msgId of the parent message being replied to (optional).
      Returns:
      whether a TwitchChat instance was found and used to send the message
    • sendMessage

      public boolean sendMessage​(java.lang.String channelToIdentifyChatInstance, java.lang.String targetChannel, java.lang.String message, @Unofficial @Nullable @Nullable java.util.Map<java.lang.String,​java.lang.Object> tags)
      Sends a message from the identified TwitchChat instance with the specified tags.
      Parameters:
      channelToIdentifyChatInstance - the channel used to identify which TwitchChat instance should be used to send the message; the instance must be subscribed to this channel.
      targetChannel - the channel to send the message to, if not null (otherwise it is sent directly on the socket).
      message - the message to be sent.
      tags - the message tags (unofficial).
      Returns:
      whether a TwitchChat instance was found and used to send the message
    • sendPrivateMessage

      public boolean sendPrivateMessage​(java.lang.String channelToIdentifyChatInstance, java.lang.String toChannel, java.lang.String message)
      Sends a whisper.
      Parameters:
      channelToIdentifyChatInstance - the channel used to identify which TwitchChat instance should be used to send the message; the instance must be subscribed to this channel.
      toChannel - the channel to send the whisper to.
      message - the message to send in the whisper.
      Returns:
      whether a TwitchChat instance was identified to send the message from.
      Throws:
      java.lang.NullPointerException - if the identified TwitchChat does not have a valid chatCredential
    • subscribe

      public java.lang.String subscribe​(java.lang.String s)
      Joins a channel.
      Specified by:
      subscribe in interface com.github.twitch4j.common.pool.TransactionalSubscriber<java.lang.String,​java.lang.String,​java.lang.Boolean>
      Overrides:
      subscribe in class com.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,​java.lang.String,​java.lang.String,​java.lang.Boolean>
      Parameters:
      s - the channel name
      Returns:
      the channel name
    • joinChannel

      public void joinChannel​(java.lang.String channelName)
      Description copied from interface: ITwitchChat
      Joins a channel
      Specified by:
      joinChannel in interface ITwitchChat
      Parameters:
      channelName - The target channel name.
    • unsubscribe

      public java.lang.Boolean unsubscribe​(java.lang.String s)
      Parts from a channel.
      Specified by:
      unsubscribe in interface com.github.twitch4j.common.pool.TransactionalSubscriber<java.lang.String,​java.lang.String,​java.lang.Boolean>
      Overrides:
      unsubscribe in class com.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,​java.lang.String,​java.lang.String,​java.lang.Boolean>
      Parameters:
      s - the channel name
      Returns:
      a non-null class if able to part, null otherwise
    • leaveChannel

      public boolean leaveChannel​(java.lang.String channelName)
      Description copied from interface: ITwitchChat
      Parts from a channel
      Specified by:
      leaveChannel in interface ITwitchChat
      Parameters:
      channelName - The target channel name.
      Returns:
      whether the channel was previously joined
    • isChannelJoined

      public boolean isChannelJoined​(java.lang.String channelName)
      Description copied from interface: ITwitchChat
      Check if Chat is currently in a channel
      Specified by:
      isChannelJoined in interface ITwitchChat
      Parameters:
      channelName - channel to check (without # prefix)
      Returns:
      boolean
    • getChannels

      public java.util.Set<java.lang.String> getChannels()
      Description copied from interface: ITwitchChat
      Returns a set of all currently joined channels (without # prefix)
      Specified by:
      getChannels in interface ITwitchChat
      Returns:
      a set of channel names
    • handleSubscription

      protected java.lang.String handleSubscription​(TwitchChat twitchChat, java.lang.String s)
      Specified by:
      handleSubscription in class com.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,​java.lang.String,​java.lang.String,​java.lang.Boolean>
    • handleDuplicateSubscription

      protected java.lang.String handleDuplicateSubscription​(TwitchChat twitchChat, TwitchChat old, java.lang.String s)
      Specified by:
      handleDuplicateSubscription in class com.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,​java.lang.String,​java.lang.String,​java.lang.Boolean>
    • handleUnsubscription

      protected java.lang.Boolean handleUnsubscription​(TwitchChat twitchChat, java.lang.String s)
      Specified by:
      handleUnsubscription in class com.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,​java.lang.String,​java.lang.String,​java.lang.Boolean>
    • getRequestFromSubscription

      protected java.lang.String getRequestFromSubscription​(java.lang.String s)
      Specified by:
      getRequestFromSubscription in class com.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,​java.lang.String,​java.lang.String,​java.lang.Boolean>
    • getSubscriptionSize

      protected int getSubscriptionSize​(java.lang.String s)
      Specified by:
      getSubscriptionSize in class com.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,​java.lang.String,​java.lang.String,​java.lang.Boolean>
    • createConnection

      protected TwitchChat createConnection()
      Specified by:
      createConnection in class com.github.twitch4j.common.pool.AbstractConnectionPool<TwitchChat>
    • disposeConnection

      protected void disposeConnection​(TwitchChat connection)
      Specified by:
      disposeConnection in class com.github.twitch4j.common.pool.AbstractConnectionPool<TwitchChat>
    • getChannelIdToChannelName

      public java.util.Map<java.lang.String,​java.lang.String> getChannelIdToChannelName()
      Note: this map does not dynamically update unlike TwitchChat.getChannelIdToChannelName()

      Specified by:
      getChannelIdToChannelName in interface ITwitchChat
      Returns:
      cached mappings of channel ids to names
    • getChannelNameToChannelId

      public java.util.Map<java.lang.String,​java.lang.String> getChannelNameToChannelId()
      Note: this map does not dynamically update unlike TwitchChat.getChannelNameToChannelId()

      Specified by:
      getChannelNameToChannelId in interface ITwitchChat
      Returns:
      cached mappings of channel names to ids
    • builder