# Extension pubsub: duplicate messages

**URL:** <https://staging-discuss.dev.twitch.com/t/extension-pubsub-duplicate-messages/15715>\
**Category:** Extensions\
**Created:** [May 10, 2018, 4:32pm UTC](https://staging-discuss.dev.twitch.com/t/extension-pubsub-duplicate-messages/15715 "2018-05-10T16:32:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![curiosityGuy](https://avatars.discourse-cdn.com/v4/letter/c/e9bcb4/32.png) [@curiosityGuy](https://staging-discuss.dev.twitch.com/u/curiosityGuy)\
**Post date:** [May 10, 2018, 4:32pm UTC](https://staging-discuss.dev.twitch.com/t/extension-pubsub-duplicate-messages/15715/1 "2018-05-10T16:32:47Z")

</div>

Hi, we have a question regarding extension pubSub System:

If we send a message to your API with targets set to [“broadcast”], this message will get delivered two times to the extension. The callback is always called two times.  
We are sure that our EBS sends the message only one time to the Twitch PubSub API.  
Our logs say that only one https-connection with the message is established.  
It is send the following way:

- target URL: [https://api.twitch.tv/extensions/message/](https://api.twitch.tv/extensions/message/)…
- JSON POST Request: {‘content\_type’: ‘application/json’, ‘message’: ‘{…}’, ‘targets’: [‘broadcast’]}
- headers: {‘Client-ID’: …, ‘Authorization’: ‘Bearer …’}

Is something wrong with this call?

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 10, 2018, 10:04pm UTC](https://staging-discuss.dev.twitch.com/t/extension-pubsub-duplicate-messages/15715/2 "2018-05-10T22:04:23Z")

</div>

> [@curiosityGuy](#):
>
> JSON POST Request: {‘content\_type’: ‘application/json’, ‘message’: ‘{…}’, ‘targets’: [‘broadcast’]}

Can you include a full example payload/call that you are sending with secrets omitted.

---

<div class="post-metadata">

**Author:** ![curiosityGuy](https://avatars.discourse-cdn.com/v4/letter/c/e9bcb4/32.png) [@curiosityGuy](https://staging-discuss.dev.twitch.com/u/curiosityGuy)\
**Post date:** [May 11, 2018, 2:22pm UTC](https://staging-discuss.dev.twitch.com/t/extension-pubsub-duplicate-messages/15715/3 "2018-05-11T14:22:32Z")

</div>

We have different messages. One exact example is:

- target URL: [https://api.twitch.tv/extensions/message/[channel\_id]](https://api.twitch.tv/extensions/message/%5Bchannel_id%5D)
- JSON POST Request: {‘content\_type’: ‘application/json’, ‘message’: ‘{“event”: “newOnlineState”, “data”: {“online”: 1}}’, ‘targets’: [‘broadcast’]}
- headers: {‘Client-ID’: [client\_id], ‘Authorization’: ‘Bearer [token]’}

Do you think it has something to do with the content? I suggest that the content does’nt matter.

The API call on EBS looks so (python):

> def Send(self, channelId, event, data, targets = [“broadcast”]):  
> headers = {  
> “Client-ID”: CLIENT\_ID,  
> “Authorization”: “Bearer %s” % (self.GenerateJWT(channelId), ),  
> }  
> messageData = {‘event’: event, ‘data’: data}  
> httpData = {“content\_type”: “application/json”, “message”: json.dumps(messageData), “targets”: targets}  
> pubSubURL = “[https://api.twitch.tv/extensions/message/%s](https://api.twitch.tv/extensions/message/%25s)” % (channelId, )  
> result = requests.post(pubSubURL, json = httpData, headers=headers)

and the call in the extension is:

> window.Twitch.ext.listen(“broadcast”, pubsubMsg);

The pubsubMsg function is called twice, during the Send function on EBS side is called once.

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d1d6mrmdu7ztse.cloudfront.net/user_avatar/staging-discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://staging-discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [May 11, 2018, 2:49pm UTC](https://staging-discuss.dev.twitch.com/t/extension-pubsub-duplicate-messages/15715/4 "2018-05-11T14:49:49Z")

</div>

> [@curiosityGuy](#):
>
> Do you think it has something to do with the content? I suggest that the content does’nt matter.

Perhaps which is why I asked. Your call seems ok.

> [@curiosityGuy](#):
>
> and the call in the extension is:
> 
> window.Twitch.ext.listen(“broadcast”, pubsubMsg);
> 
> The pubsubMsg function is called twice, during the Send function on EBS side is called once.

Where in your extension is the listen call?

Are you doing something like, what I’ve done before and caused your listen to be called more than once? As per:

> [@Calling listen inside onAuthorized, listen callback'ed four times](https://staging-discuss.dev.twitch.com/t/calling-listen-inside-onauthorized-listen-callbacked-four-times/12032):
>
> Consider this code: var authed = false; window.Twitch.ext.onAuthorized(function(auth) { console.log('Authorised'); if (authed) { return; } authed = true; window.Twitch.ext.listen('broadcast', function (topic, contentType, message) { console.log('Recv'); // do stuff }); }); Compared to var authed = false; window.Twitch.ext.onAuthorized(function(auth) { console.log('Authorised'); if (authed) { return; } authed = true; }); …

I ended up having my listen callback called four times. A `listen` being called inside `onAuthorized` causes unpredictable results and/or multiple listeners being bound.

---

<div class="post-metadata">

**Author:** ![curiosityGuy](https://avatars.discourse-cdn.com/v4/letter/c/e9bcb4/32.png) [@curiosityGuy](https://staging-discuss.dev.twitch.com/u/curiosityGuy)\
**Post date:** [May 12, 2018, 8:47am UTC](https://staging-discuss.dev.twitch.com/t/extension-pubsub-duplicate-messages/15715/5 "2018-05-12T08:47:30Z")

</div>

Exactly that is what I do - I call the listen function inside onAuthorized callback.

I putted it outside now and now it works.  
This looks like a bug on twitch side. Maybe it makes sense to provide this constrain in the documentation or just solve the bug 🙂 (seems to be an old bug)

Thanks a lot for your help.
