Usage
Generate token on site:
// generate random token:
fetch('https://ningi.sttv.me/about/gen', {
headers: {
'Content-Type': 'application/json'
},
method: 'GET',
}).then(response => response.json()).then(response => {
console.log(response.uuid);
console.log(response.token);
});
// OR token with custom uuid (for example: f3d9438e-103c-4e6d-81cf-1091d18b3689)
fetch('https://ningi.sttv.me/about/gen/f3d9438e-103c-4e6d-81cf-1091d18b3689', {
headers: {
'Content-Type': 'application/json',
},
method: 'GET',
}).then(response => response.json()).then(response => {
console.log(response.uuid);
console.log(response.token);
});
Connect to websocket server:
const token = '<token>';
var ws;
fetch('https://ningi.sttv.me/register', {
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify({
token: token, // jwt token
topics: ["my-topic", "cat"],
})
}).then(response => response.json()).then(response => {
const url = response.url;
console.log(url);
ws = new WebSocket(url);
ws.onopen = () => {
ws.send('{"topics": ["my-topic","cat","mouse"]}'); // re-subscribe
ws.onmessage = ()=>{};
ws.onerror = ()=>{} // console.log(arguments)
};
});
Publish events:
const secret = '<secret>'; // see "Example secrets"
// generate random token:
fetch('https://ningi.sttv.me/publish', {
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify({
"topic": "my-awesome-topic", // see
"user_ids": ["<uuid>", ...],
"message": "My awesome message",
"key": "",
})
}).then(response => response.json()).then(response => {
console.log(response);
});