cytube-client
A simple Promise-based, non-blocking socket.io client for retrieving information from cytu.be servers with NodeJS. Supports async/await, then/catch, and callbacks.
Designed to work with sync 3.7+
. The socket.io-client
version used by cytube-client
must match the version used by the sync
server exactly.
This package is tested against Node LTS and Latest (at time of release), but should be compatible with all Node versions > 6.0
.
Installation
npm install cytube-client
or
yarn add cytube-client
Usage
With Promises
let cyClient;
try {
cyClient = await require('cytube-client').connect('channel');
} catch(err) {
}
await cyClient.getCurrentMedia();
cyClient.on('changeMedia', data => {
});
/ ...
cyClient.close();
With Callbacks
var cytube = require('cytube-client');
cytube.connect('channel', (err, client) => {
if(err) {
}
client.getCurrentMedia((err, data) => {
if(err) {
}
else {
console.log(data);
}
});
client.on('changeMedia', (data) => {
console.log(data);
});
client.close();
});
Sync allows clients to join channels that do not already exist. The server will begin sending events if they are created, but will not prevent the connection.
Please ensure your channel names are accurate if requests are timing out.
Connection Options
var options = {
channel: 'channel',
password: 'password',
secure: true,
reconnect: true,
socketServer: 'https://your.sync.server:3000',
timeout: 15000
};
var cytube = require('cytube-client');
cytube.connect(options).then(connection => {
});
Retrieving Channel Information
Cytube-client provides some basic Promise-based functions for retrieving channel state information, since sync no longer has a built-in REST API.
These functions can also be used with callbacks.
client.getCurrentMedia();
client.getPlaylist();
client.getUserlist();
Event Listeners
The socket.io client is exposed at client.socket
and can be used to attach event listeners. See the socket.io-client documentation for more information.
client.on('event', callback);
client.once('event', callback);
client.off('event');
For a full list of emitted events, see the sync documentation. Basic events include:
changeMedia
queue
chatMsg
addUser
userLeave
Contributions
Contributions and pull requests are always welcome. Please be sure your code passes all existing tests and linting.
Pull requests with full code coverage are strongly encouraged.
An integration test is provided for testing connections to cytu.be itself. Because cytu.be channels are ephemeral by nature, this test is not run automatically as part of the normal npm test
suite.
To run the integration test, use CYTUBE_TEST_CHANNEL="your-channel-here" npm run testIntegration
. You can also set DEBUG=socket.io*
or DEBUG=engine,socket.io*
to receive debug output from the websocket when running this test.
License
MIT