Class TwitchPubSub

java.lang.Object
com.github.twitch4j.pubsub.TwitchPubSub
All Implemented Interfaces:
ITwitchPubSub, java.lang.AutoCloseable

public class TwitchPubSub
extends java.lang.Object
implements ITwitchPubSub
Twitch PubSub
  • Field Details

    • REQUIRED_THREAD_COUNT

      public static final int REQUIRED_THREAD_COUNT
      See Also:
      Constant Field Values
    • queueTask

      protected final java.util.concurrent.Future<?> queueTask
      Command Queue Thread
    • heartbeatTask

      protected final java.util.concurrent.Future<?> heartbeatTask
      Heartbeat Thread
    • isClosed

      protected volatile boolean isClosed
      is Closed?
    • commandQueue

      protected final java.util.concurrent.BlockingQueue<java.lang.String> commandQueue
      Command Queue
    • subscribedTopics

      protected final java.util.Set<PubSubRequest> subscribedTopics
      Holds the subscribed topics in case we need to reconnect
    • lastPing

      protected volatile long lastPing
      Last Ping send (1 minute delay before sending the first ping)
    • lastPong

      protected volatile long lastPong
      Last Pong received
    • taskExecutor

      protected final java.util.concurrent.ScheduledExecutorService taskExecutor
      Thread Pool Executor
    • webSocketFactory

      protected final com.neovisionaries.ws.client.WebSocketFactory webSocketFactory
      WebSocket Factory
    • backoff

      protected final com.github.twitch4j.common.util.ExponentialBackoffStrategy backoff
      Helper class to compute delays between connection retries.

      Configured to (approximately) emulate first-party clients:

      • initially waits one second
      • plus a small random jitter
      • doubles the backoff period on subsequent failures
      • up to a maximum backoff threshold of 2 minutes
      See Also:
      Official documentation - Handling Connection Failures
  • Constructor Details

    • TwitchPubSub

      public TwitchPubSub​(com.github.philippheuer.events4j.core.EventManager eventManager, java.util.concurrent.ScheduledThreadPoolExecutor taskExecutor, com.github.twitch4j.common.config.ProxyConfig proxyConfig, java.util.Collection<java.lang.String> botOwnerIds)
      Constructor
      Parameters:
      eventManager - EventManager
      taskExecutor - ScheduledThreadPoolExecutor
      proxyConfig - ProxyConfig
      botOwnerIds - Bot Owner IDs
  • Method Details

    • connect

      public void connect()
      Connecting to IRC-WS
    • disconnect

      public void disconnect()
      Disconnecting from WebSocket
    • reconnect

      public void reconnect()
      Reconnecting to WebSocket
    • listenOnTopic

      public PubSubSubscription listenOnTopic​(PubSubRequest request)
      Description copied from interface: ITwitchPubSub
      Send WS Message to subscribe to a topic
      Specified by:
      listenOnTopic in interface ITwitchPubSub
      Parameters:
      request - Topic
      Returns:
      PubSubSubscription to be listened to
    • unsubscribeFromTopic

      public boolean unsubscribeFromTopic​(PubSubSubscription subscription)
      Description copied from interface: ITwitchPubSub
      Unsubscribe from a topic. Usage example:
            PubSubSubscription subscription = twitchPubSub.listenForCheerEvents(...);
            // ...
            twitchPubSub.unsubscribeFromTopic(subscription);
       
      Specified by:
      unsubscribeFromTopic in interface ITwitchPubSub
      Parameters:
      subscription - PubSubSubscription to be unlistened from
      Returns:
      whether the subscription was previously subscribed to
    • close

      public void close()
      Close
      Specified by:
      close in interface java.lang.AutoCloseable
      Specified by:
      close in interface ITwitchPubSub
    • getEventManager

      public com.github.philippheuer.events4j.core.EventManager getEventManager()
      EventManager
      Specified by:
      getEventManager in interface ITwitchPubSub