Topic - Onsite Notifications
Description
This topic can be used to monitor notifications that would occur on the twitch website (e.g. if a stream goes live).
With all undocumented topics, use at your own risk.
Method Definition
Name | Type | Description |
---|---|---|
credential | OAuth2Credential | User Auth Token for the target user id |
userId | String | Target User Id |
Code-Snippets
Example: General use
Subscribe to all onsite notifications for user twitch4j
and register listeners that print all messages to console
- Java
- Kotlin
- Groovy
// Subscribe to the topic
twitchClient.getPubSub().listenForOnsiteNotificationEvents(credential, "149223493");
// Listen to notification creations
twitchClient.getEventManager().onEvent(OnsiteNotificationCreationEvent.class, System.out::println);
// Listen for notification summary updates
twitchClient.getEventManager().onEvent(UpdateOnsiteNotificationSummaryEvent.class, System.out::println);
// Subscribe to the topic
twitchClient.pubSub.listenForOnsiteNotificationEvents(credential, "149223493");
// Listen to notification creations
twitchClient.eventManager.onEvent(OnsiteNotificationCreationEvent::class.java, System.out::println);
// Listen for notification summary updates
twitchClient.eventManager.onEvent(UpdateOnsiteNotificationSummaryEvent::class.java, System.out::println);
// Subscribe to the topic
twitchClient.pubSub.listenForOnsiteNotificationEvents(credential, "149223493");
// Listen to notification creations
twitchClient.eventManager.onEvent(OnsiteNotificationCreationEvent, System.out::println);
// Listen for notification summary updates
twitchClient.eventManager.onEvent(UpdateOnsiteNotificationSummaryEvent, System.out::println);
Example: Live status monitoring
Note: a single channel can be following up to 2000 channels, as of the time of writing. Such events cannot be fired unless the notifications bell is enabled for the channel.
Note: these notifications tend to be fired faster than repeatedly querying API - Helix > Streams Get, as TwitchClientHelper
(Twitch4J > Client Helper) does.
warning
Disclaimer: Do not solely rely upon this code; fallback mechanisms should be employed as well.
- Java
- Kotlin
- Groovy
// Establish which channels' live state is relevant
Set<String> interestedChannelIds = new HashSet<>();
interestedChannelIds.add("12826");
interestedChannelIds.add("53888434");
interestedChannelIds.add("141981764");
interestedChannelIds.add("142621956");
// Subscribe to the topic
twitchClient.getPubSub().listenForOnsiteNotificationEvents(credential, userId);
// Listen to notification creations
twitchClient.getEventManager().onEvent(OnsiteNotificationCreationEvent.class, e -> {
OnsiteNotification notification = e.getData().getNotification();
if (notification.getType().equalsIgnoreCase("streamup")) {
List<OnsiteNotification.Creator> creators = notification.getCreators();
if (creators != null && creators.size() == 1 && interestedChannelIds.contains(creators.get(0).getUserId())) {
System.out.println(e); // Handle Go Live
}
}
});
// Establish which channels' live state is relevant
val interestedChannelIds = setOf("12826", "53888434", "141981764", "142621956")
// Subscribe to the topic
twitchClient.pubSub.listenForOnsiteNotificationEvents(credential, userId);
// Listen to notification creations
twitchClient.eventManager.onEvent(OnsiteNotificationCreationEvent::class.java) { e ->
val notification = e.`data`.notification;
if (notification.type.equals("streamup", true)) {
notification.creators?.apply {
if (size == 1 && interestedChannelIds.contains(get(0).userId) {
println(e); // Handle Go Live
}
}
}
}
// Establish which channels' live state is relevant
def interestedChannelIds = new HashSet<>()
interestedChannelIds.add("12826")
interestedChannelIds.add("53888434")
interestedChannelIds.add("141981764")
interestedChannelIds.add("142621956")
// Subscribe to the topic
twitchClient.pubSub.listenForOnsiteNotificationEvents(credential, userId)
// Listen to notification creations
twitchClient.eventManager.onEvent(OnsiteNotificationCreationEvent) { e ->
def notification = e.data.notification
if (notification.type.equalsIgnoreCase("streamup")) {
def creators = notification.creators
if (creators != null && creators.size() == 1 && interestedChannelIds.contains(creators[0].userId)) {
System.out.println(e) // Handle Go Live
}
}
}