Skip to content

Instantly share code, notes, and snippets.

@lee-brown
Created March 18, 2019 15:54
Show Gist options
  • Save lee-brown/a2332fd8861c156a5987c1b22667eae8 to your computer and use it in GitHub Desktop.
Save lee-brown/a2332fd8861c156a5987c1b22667eae8 to your computer and use it in GitHub Desktop.
Mixer Example
'use strict';
const Mixer = require('@mixer/client-node');
const ws = require('ws');
const client = new Mixer.Client(new Mixer.DefaultRequestRunner());
let userInfo;
let userId;
let channelId;
let endpoints;
let authkey;
client.use(new Mixer.OAuthProvider(client, {
tokens: {
access: '',
expires: Date.now() + (365 * 24 * 60 * 60 * 1000)
},
}));
// Gets the user that the Access Token we provided above belongs to.
client.request('GET', 'users/current')
.then(response => {
console.log(response.body);
userInfo = response.body;
userId = response.body.id;
channelId = response.body.channel.id
return new Mixer.ChatService(client).join(response.body.channel.id);
})
.then(response => {
const body = response.body;
endpoints = response.body.endpoints
authkey = response.body.authkey
console.log(body);
console.log(userId + " " + channelId + " " + endpoints + " " + authkey)
createChatSocket(userId, channelId, endpoints, authkey)
})
.catch(error => {
console.error('Something went wrong.');
console.error(error);
});
function createChatSocket (userId, channelId, endpoints, authkey) {
const socket = new Mixer.Socket(ws, endpoints).boot();
// You don't need to wait for the socket to connect before calling
// methods. We spool them and run them when connected automatically.
socket.auth(channelId, userId, authkey)
.then(() => {
console.log('You are now authenticated!');
// Send a chat message
return socket.call('msg', ['Hello world!']);
})
.catch(error => {
console.error('Oh no! An error occurred.');
console.error(error);
});
// Listen for chat messages. Note you will also receive your own!
socket.on('ChatMessage', data => {
console.log('We got a ChatMessage packet!');
console.log(data);
console.log(data.message); // Let's take a closer look
});
// Listen for socket errors. You will need to handle these here.
socket.on('error', error => {
console.error('Socket error');
console.error(error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment