1 | # CatSnake JS
|
2 |
|
3 | CatSnake JS - Open source, free to use persistant pubsub for building realtime applications.
|
4 |
|
5 | ## What is CatSnake JS?
|
6 |
|
7 | CatSnake JS is a PubSub solution that allows realtime communications between clients and
|
8 | devices. With CatSnake you can simply subscribe a device to a channel and any time
|
9 | a JavaScript object is published to that channel your device or client will be notified.
|
10 |
|
11 | The best part is Catsnake is built using WebSockets and
|
12 | [MessagePack](http://msgpack.org) so it's incredibly fast.
|
13 |
|
14 |
|
15 | ## Something worth noting
|
16 |
|
17 | Catsnake will soon feature pluggable modules for things like Mapping, IOT and more.
|
18 | In addition we will have libraries for Python and Java, as well as native implementations for mobile development. Stay tuned :)
|
19 |
|
20 | ## Usage
|
21 |
|
22 | ### Setup
|
23 |
|
24 | If you're using npm you can simply require and create a catsnake client.
|
25 | ```javascript
|
26 | const CatSnake = require('catsnake');
|
27 | const catsnake = new CatSnake('ws://public.catsnake.io', {
|
28 | // The common name is how people will know who or what a client is.
|
29 | // If no commonName is provided your client will be known as A Random Catsnake
|
30 | commonName: 'A Random Catsnake'
|
31 | });
|
32 |
|
33 | ```
|
34 |
|
35 | If you're using this without a module builder just go ahead and use the following without the require. CatSnake will
|
36 | already be defined.
|
37 | ```javascript
|
38 | const catsnake = new CatSnake('ws://public.catsnake.io', {
|
39 | // The common name is how people will know who or what a client is.
|
40 | // If no commonName is provided your client will be known as A Random Catsnake
|
41 | commonName: 'A Random Catsnake'
|
42 | });
|
43 | ```
|
44 |
|
45 |
|
46 | ### Subscribe
|
47 | 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.
|
48 |
|
49 | ```javascript
|
50 | CatSnake.subscribe('General', msg => console.log(msg));
|
51 | ```
|
52 |
|
53 | There are ofcourse additional options avalible.
|
54 | ```javascript
|
55 | CatSnake.subscribe('General', msg => console.log(msg), {
|
56 | // If you're passing around some private information you can make your channel private by simply passing in a privateKey.
|
57 | // Anyone who want's to later publish to this channel will need this key to do so!
|
58 | privateKey: 'ShhThisIsAPrivateChannel',
|
59 | // If you would like to be able to block clients in the future, or promote
|
60 | // other clients to be able to manage blocked users etc, add a secret.
|
61 | secret: 'MySuperSecretKey',
|
62 | // If you want the channel to be invite only, pass in private as true, this
|
63 | // is different from the privateKey, with the private key all you need to do
|
64 | // is pass the privateKey to an authorized user to grant them pubsub access
|
65 | // with private you need to grant them access manually by clientID, which
|
66 | // they will need to send to your client before trusting, this is not
|
67 | // publicly avalible and creates a handshake.
|
68 | private: false
|
69 | }
|
70 | ```
|
71 |
|
72 | ### Publish
|
73 | Now that you've subscibed to a channel, let's publish a message to all of the other subscribers, and ourselves ofcourse.
|
74 |
|
75 | ```javascript
|
76 | CatSnake.publish('General', msg => {
|
77 | message: 'Ahh! Your dog is attacking me! What is it with mail men and dogs anyways?'
|
78 | });
|
79 | ```
|
80 |
|
81 |
|
82 | Whoops! It looks like the general channel is private. Let's pass in the privateKey the same way we do in our subscribe method.
|
83 | ```javascript
|
84 | CatSnake.publish('General', msg => {
|
85 | message: 'Ahh! Your dog is attacking me! What is it with mail men and dogs anyways?'
|
86 | }, 'ShhThisIsAPrivateChannel');
|
87 | ```
|
88 |
|
89 |
|
90 | ### Access Control
|
91 |
|
92 | Catsnake gives you multiple options for access control to a channel. We'll talk
|
93 | about denying access to a channel as well as granting access.
|
94 |
|
95 | Denying a client access to a channel protected by access control is easy
|
96 | ```javascript
|
97 | CatSnake.deny('General', 'client-123215', 'secretKey');
|
98 | ```
|
99 |
|
100 | Granting a client access to a channel protected by access control is just as simple.
|
101 | ```javascript
|
102 | CatSnake.grant('General', 'client-123215', 'secretKey');
|
103 | ```
|
104 |
|
105 | ### Info
|
106 |
|
107 | Nice, now let's get info from the channel, this will return information like connected clients, author, etc.
|
108 |
|
109 | ```javascript
|
110 | CatSnake.info('General');
|
111 | ```
|
112 |
|
113 | ### Unsubscribe
|
114 | Well that was easy. Let's go over one last thing, before we get into the more advanced features of jsps.
|
115 | Once you're done pubsubbing you can unsubscribe from the channel. This will leave your client in an offline state but
|
116 | you can later reconnect with the same client id via the subscribe method, we will go over this more in the advanced
|
117 | features below.
|
118 |
|
119 | ```javascript
|
120 | CatSnake.unsubscribe('General');
|
121 | ```
|
122 |
|
123 | ### History
|
124 |
|
125 | Wanna get some history? Just put the channel, the limit, and a privateKey if the channel is private.
|
126 | History will be sent back in the subscriber function, you can check the metadata.type for the type of 'history'
|
127 | ```javascript
|
128 | CatSnake.history('General', limit, {
|
129 | privateKey: 'ShhThisIsAPrivateChannel' // Optional
|
130 | });
|
131 | ```
|