Skip to content

DevRig: PubSub Broadcasts for non-default topics are not received by listeners #142

@lettucemode

Description

@lettucemode

Brief description
When sending a PubSub Broadcast, if the topic/target is something besides "broadcast" or "global" (for example, "whisper-opaque_user_id"), the broadcast is not received by simulated user views in the Developer Rig.

How to reproduce
Frontend code to listen for broadcasts:

twitch.onAuthorized(function (auth) {
  twitch.listen('whisper-' + auth.userId, function (targ, cType, message) {
   twitch.rig.log(message); // this is never hit
  });
});

Backend code to send a PubSub broadcast (C#):

public async Task SendPubSubWhisper(string channelId, IEnumerable<string> userIds, string message)
{
    using var request = new HttpRequestMessage(HttpMethod.Post,
        $"https://api.twitch.tv/extensions/message/{channelId}");
    request.Headers.Add("Client-ID", this._extClientId);
    request.Headers.Add("Authorization", BEARER_PREFIX + this.MakePubSubToken(channelId, true));

    var body = new Dictionary<string, object>
    {
        { "content_type", "application/json" },
        { "message", message },
        { "targets", userIds.Select(uid => $"whisper-{uid}").ToArray() },
    };
    request.Content = new StringContent(
        new JsonNetSerializer().Serialize(body), Encoding.UTF8, "application/json");

    using var client = new HttpClient();
    using var response = await client.SendAsync(request);
}

private string MakePubSubToken(string channelId, bool isWhisper)
{
    var perms = new perms { send = new string[] { isWhisper ? "whisper-*" : "*" } };
    var payload = new Dictionary<string, object>
    {
        { "exp", (DateTimeOffset.Now.ToUnixTimeSeconds() + 60) },
        { "user_id", this._extOwnerId },
        { "role", "external" },
        { "channel_id", channelId },
        { "pubsub_perms", perms }
    };

    IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
    IJsonSerializer serializer = new JsonNetSerializer();
    IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
    IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
    var token = encoder.Encode(payload, Convert.FromBase64String(this._extSecret));
    return token;
}

Expected behavior
Extensions running in the Developer Rig should receive PubSub broadcast events for any topic/target.

Additional context or questions
The example code above DOES work as expected for all topics/targets when running the extension as a Hosted Test in Twitch. In either case the response from the PubSub Broadcast endpoint is 204 No Content.

Metadata

Metadata

Assignees

No one assigned

    Labels

    product: developer rigDecommissionedstopped investigatingAn issue that cannot be reproduced and is no longer being actively investigated.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions