The Twitch PubSub documentation (PubSub | Twitch Developers) has an incorrect example for Channel Points Redemption. As can be seen from every other example, the real message sent by Twitch contains the message type (“MESSAGE”) and topic (like “channel-subscribe-events-v1”). However, the example for Channel Points Redemption omits this which makes it hard to debug/troubleshoot.
As a side question, this makes me wonder if the formatting of Channel Points Redemption is any different from other examples (such as “Channel Subscriptions Event Message”). For Channel Points Redemption, I’ve noticed that the message is doubly JSON encoded. For example, the following code does NOT work (although it would seem like it should based on the examples provided by Twitch).
onmessage(event) {
display_name = JSON.parse(event.data).data.message.redemption.user.display_name;
}
Instead, you need to do the following
onmessage(event) {
display_name = JSON.parse(JSON.parse(event.data).data.message).data.redemption.user.display_name;
}
It would be helpful if all the examples described this nesting correctly; which would dictate that the nested JSON objects should be interpreted as strings.
– EDIT –
I just noticed that other examples (like Bits) actually does include the full nested examples. Channel Redemptions and Channel Subscriptions examples opted to show it more visually elegant but in a misleading (and inconsistent) way. Channel Subscriptions examples includes the topic/message in the print as where Channel Redemptions does not.
With that in mind, let me make my suggestions more clear:
- Add a raw example for Channel Points & Channel Subscriptions messages (similar to the raw examples provided for “Bits Event v1 Message”).
- Either include topic/message type in both Channel Points & Channel Subscriptions visually elegant example OR don’t show it in either. Currently, one has it (subscriptions) and one does not (channel points).