README
Sockethub Client
This library handles all the nitty-gritty details of talking to your sockethub.
Terms
sockethub-client is dual-licensed under either the MIT License or GPLv3 (at your choice).
Getting started
Include sockethub-client.js script:
<script src="sockethub-client.js"></script>
Create a client and wait for the "registered" event to fire:
var sockethubClient = SockethubClient.connect({
host: 'localhost',
register: {
secret: '1234567890'
}
})
sockethubClient.on('registered', function() {
// done!
// you can start calling verbs now, such as...
console.log('ping');
sockethubClient.ping().then(function() {
console.log('pong');
}, function(response) {
console.log('ping failed: ', response.message);
});
});
When using ssl, add a field ssl: true
to the options in the sockethubClient.connect
call.
You may also want to set up some error handlers:
sockethubClient.on('failed', function() {
console.error("Connection to sockethub failed!");
});
sockethubClient.on('disconnected', function() {
console.error("Sockethub got disconnected!");
});
for more information, see: Quickstart
Events
Out of the box, SockethubClient
implements these events:
connected
- Fired once the WebSocket connection is established (i.e. socket.onopen has been called)failed
- Fired when the WebSocket connection failed to be establisheddisconnected
- Fired when the WebSocket connection is closed (but it had been connected)reconnecting
- Fired when the WebSocket connection is closed, but configuration is set to reconnect (default)message
- Fired when the hub sends a message that isn't associated to a specific request.unexpected-response
- Fired when a response is received that carries a "rid" attribute, but that "rid" isn't known to us. This is an error case (and probably indicates a bug in the corresponding sockethub platform).registered
- Fired whenregister
succeeds.registration-failed
- Fired whenregister
fails.
Properties
The following properties are available for your use:
options
- This is the options object that was passed toSockethubClient.connect
.connected
- Boolean property reflecting the connection state of the socket.registered
- Boolean property. Initially false, set to the result of theregister
command once that returns.
RequireJS
Using sockethub-client with an AMD loader, such asCurrently the AMD module and the simple client are separate code-bases. For more info on using Sockethub with AMD see the doc here: AMD