Hi !
I don’t know why my API doesn’t works anymore. It was working well before this week, pubsub always returns me the first thing i sent to it
Can someone help me ?
Thanks !
Hi !
I don’t know why my API doesn’t works anymore. It was working well before this week, pubsub always returns me the first thing i sent to it
Can someone help me ?
Thanks !
Please show us your code
Thanks for your response ! There is my code
<script type="text/javascript">
$(document).ready(function() {
const clientId = 'XXX'
const token = 'XXX'
connect();
// Source: https://www.thepolyglotdeveloper.com/2015/03/create-a-random-nonce-string-using-javascript/
function nonce(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
function heartbeat() {
var message = {
type: 'PING'
};
console.log('SENT: ' + JSON.stringify(message));
ws.send(JSON.stringify(message));
}
function listen_pubsub(topic) {
message = {
type: 'LISTEN',
nonce: nonce(15),
data: {
topics: topic,
auth_token: token
}
};
console.log('SENT: ' + JSON.stringify(message) + '\n');
ws.send(JSON.stringify(message));
}
function connect() {
if (token) {
var heartbeatInterval = 1000 * 60; //ms between PING's
var reconnectInterval = 1000 * 3; //ms to wait before reconnect
var heartbeatHandle;
ws = new WebSocket('wss://pubsub-edge.twitch.tv');
ws.onopen = function(event) {
console.log('INFO: Socket Opened');
heartbeat();
heartbeatHandle = setInterval(heartbeat, heartbeatInterval);
$.ajax({
url: "https://api.twitch.tv/helix/users",
crossDomain: true,
method: "GET",
headers: {
"Accept": "application/vnd.twitchtv.v5+json",
"Authorization": "Bearer "+token
}})
.done(function(user) {
topics = []
// These are Laravel conditions that works
@if($user->bits == 1)
topics.push("channel-bits-events-v1."+user.data[0].id);
@endif
@if($subsCustomisation)
topics.push("channel-subscribe-events-v1."+user.data[0].id);
@endif
listen_pubsub(topics)
});
};
ws.onerror = function(error) {
console.error('ERR: ' + JSON.stringify(error) + '\n');
ws.close()
window.refresh()
};
ws.onmessage = function(event) {
console.log(message)
console.log('RECV: ' + JSON.stringify(message) + '\n');
if (event.type == 'RECONNECT') {
$('.ws-output').append('INFO: Reconnecting...\n');
setTimeout(connect(), reconnectInterval);
}
else if(event.type == 'MESSAGE'){
switch(event.data.topic.split('.')[0]){
case('channel-bits-events-v1') :
message = JSON.parse(event.data.message).data;
// calling another function
break;
case('channel-subscribe-events-v1') :
message = event.data.message;
// calling another function
break;
}
}
};
ws.onclose = function() {
console.log('INFO: Socket Closed\n');
clearInterval(heartbeatHandle);
console.log('INFO: Reconnecting...\n');
setTimeout(connect(), reconnectInterval);
};
}
}
});
</script>
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.