What is CatSnake JS?
CatSnake JS is a package built on top of WebSockets to allow for a publish & subscribe style messaging for quickly building fast realtime applications. Catsnake also utilizes MessagePack so package sizes are very small making Catsnake very fast.
Docs
For detailed documentation visit this link.
Quickstart Guide
Setup
For quick use you can simply include catsnke in your <head>
.
<script type="text/javascript" src="https://cdn.jsdelivr.net/catsnake/0.2.5/catsnake.js"></script>
If you're using npm you can simply require and create a catsnake client. If you're using Catsnake via an inline script tag, simply omit the require('catsnake')
line.
const CatSnake = require('catsnake');
const cs = new CatSnake('ws://catsnake.io:3081', {
commonName: 'A Random Catsnake'
});
Methods
Subscribe
Before you can start sending messages, you should subscribe to a channel (but you don't have to). Don't worry, it's super easy.
cs.subscribe('General', data => console.log(data));
Publish
Now that you've subscibed to a channel, let's publish a message to all of the other subscribers, and ourselves ofcourse.
cs.publish('General', {
message: 'Ahh! Your dog is attacking me! What is it with mail men and dogs anyways?'
});
History
Wanna fetch some history? Easy.
cs.history('General', limit);
History will be sent back in the subscribe method with the metadata.type
of 'history'.
Info
Nice, now let's get info from the channel, this will return information like connected clients, author, etc.
cs.info('General');
Access Control
Catsnake gives you multiple options for access control to a channel.
Denying a client access to a channel protected by access control is easy
cs.deny('General', 'client-123215', 'secretKey');
Granting a client access to a channel protected by access control is just as simple.
cs.grant('General', 'client-123215', 'secretKey');
Unsubscribe
Before exiting your application you should unsubscribe from the channel. This will leave your client in an offline state but
you can later reconnect with the same client id via the new Catsnake
method, we will go over this more in the advanced
features below.
cs.unsubscribe('General');