PubSub: Bits in JS. Bits message not interpreted correctly

I’m developing a custom bits widget, and I can’t seem to properly interpret an incoming Bits message, despite having read the JSON template and trying to interpret accordingly.

Before getting into the problem, here is my socket handler layer:

function InterpretMessage(message) {

    var parsed = JSON.parse(message.data);

    // Other message type handlers.

    if (parsed.type == "MESSAGE")
    {
        for (var i = 0; i < messageCallback.length; i++)
        {
            if (messageCallback[i].topic == parsed.data.topic)
            {
                messageCallback[i].callback(parsed.data.message);
                return;
            }
        }
    }
}

This layer simply receives a message, and then looks for a callback associated with the given topic. “message” as received by the function is a MessageEvent object.

When I ask my socket handler to listen to a topic, I provide a callback to associate to it. In the case of bits, I provide the following function as the callback:

function InterpretData(message)
{
    var username = message.user_name;
    var bitsUsed = message.bits_used;
    var action = message.context;
    
    if (nextBoss == "")
    {
        // Performs a web call to obtain the display name of the person who sent bits.
        // Once finished, the webcall function then calls the
        GetUserInfo(message.user_name, function(info) {
            
            var amount = "";
            
            if (message.bits_used < 100) { amount = "1"; }
            else if (message.bits_used < 1000) { amount = "100"; }
            else if (message.bits_used < 5000) { amount = "1000"; }
            else if (message.bits_used < 10000) { amount = "5000"; }
            else { amount = "10000"; }
            
            // Do some document manipulation
            
            Strike(username, bitsUsed, info.displayName);
        });
    }
}

The problem I’m having is that the Strike command, when dealing with real bits, is always given either null or undefined values. In the demo mode I have, these values are fine since I’m creating the objects myself. This tells me that the message object received by PubSub is not the same as the message object I create for my demo, and also not the same as the message object the twitch docs say I will receive. (shown here: https://dev.twitch.tv/docs/v5/guides/PubSub/bits/ )

Since I can’t receive bits, I cannot test this with a real bits message.

Based on other topics I’m subscribed to the message field may be a JSON encoded string containing the object, instead of the object directly. Although it certainly doesn’t look that way in the docs, so I don’t know if that’s the case here.

Well, the MessageEvent’s data object was a JSON encoded string, but as you can see in my socket handler’s code, I parse that. So the resulting “parsed” variable should contain the following information as objects, numbers, and string vales:

{
    type: "MESSAGE",
    data:
    {
        topic: "channel-bitsevents.32945820",
        message:
        {
            user_name: "nifty255",
            bits_used: 100",
            etc: "and so on"
    }
}

My guess is the object doesn’t have the structure the docs say it has.

This is an example of a PubSub message I’m referring to:
{"type":"MESSAGE","data":{"topic":"chat_moderator_actions.xxx.xxx","message":"{\"data\":{\"type\":\"chat_channel_moderation\",\"moderation_action\":\"host\",\"args\":[\"xxx\"],\"created_by\":\"xxx\",\"msg_id\":\"\",\"target_user_id\":\"\"}}"}}

Notice how the message field is an object encoded in a string?

If you have someone reporting back errors, can’t they give you an example of a raw message from a debug output or something to verify what’s going on?

That’s the thing. I tried getting people to help me, but they all appeared to be too busy to help, despite making it clear that the process would have been simple and quick. The one time I did get a streamer’s attention, I held it for about 2 minutes.

But I see that you have given me exactly what I needed. An example message. Now I can go back and test with that! It may not be bits events, but it’s up the same alley!

EDIT: Turns out PubSub whispers do the same thing. It seems every object inside a message is stringified before becoming part of its parent, all the way up until the root object, which is itself stringified before being sent.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.