|
1 | 1 | <h1 align="center">ClusterWS JavaScript Client</h1> |
2 | | -<h6 align="center">WebSocket & Node JS Cluster</h6> |
| 2 | +<h6 align="center">Build Scalable Node.js WebSocket Applications</h6> |
3 | 3 |
|
4 | 4 | <p align="center"> |
5 | | - <img alt="Node.js" src="http://u.cubeupload.com/goriunovd/clusterWS.png" width="560"/> |
| 5 | + <img src="https://cdn.rawgit.com/goriunov/159120ca6a883d8d4e75543ec395d361/raw/f4c3c36ac1ab75beedcf73312272b60dac33ecfa/clusterws.svg" width="500"> |
6 | 6 | </p> |
7 | 7 |
|
8 | 8 | <p align="center"> |
9 | 9 | <a title="NPM Version" href="https://badge.fury.io/js/clusterws-client-js"><img src="https://badge.fury.io/js/clusterws-client-js.svg"></a> |
10 | 10 | <a title="GitHub version" href="https://badge.fury.io/gh/goriunov%2FClusterWS-Client-JS"><img src="https://badge.fury.io/gh/goriunov%2FClusterWS-Client-JS.svg"></a> |
11 | 11 | </p> |
12 | 12 |
|
13 | | -**This README, logo and animation will be changed soon, we are currently implementing new GUIDES in wikis and working with new logo and animation** |
14 | | - |
15 | | -## Overview |
16 | | -This is official JavaScript client for [ClusterWS](https://github.com/ClusterWS/ClusterWS). |
17 | | - |
18 | | -[ClusterWS](https://github.com/ClusterWS/ClusterWS) - is a minimal **Node JS http & real-time** framework which allows to scale WebSocket ([uWS](https://github.com/uNetworking/uWebSockets) - one of the fastest WebSocket libraries) between **Workers** in [Node JS Cluster](https://nodejs.org/api/cluster.html) and utilize all available CPU. |
19 | | - |
20 | | -**Current minified version is under 6KB.** |
21 | | - |
22 | | -**This library requires [ClusterWS](https://github.com/ClusterWS/ClusterWS) on the server** |
23 | | - |
24 | | -## Installation |
25 | | -To install ClusterWS Client JS run: |
26 | | -```js |
27 | | -npm install --save clusterws-client-js |
28 | | -``` |
29 | | -or use globally: |
30 | | - |
31 | | -1. Find `ClusterWS.(min).js` in `dist/browser` folder. |
32 | | -2. Use standard script tag to import library `<script src="path/to/ClusterWS.[min].js"></script>`. |
33 | | -3. Done, now you can use it as `ClusterWS`. |
34 | | - |
35 | | - |
36 | | -## Socket |
37 | | -### 1. Connecting |
38 | | -You can connect to the server with the following code: |
39 | | -```js |
40 | | -var cws = new ClusterWS({ |
41 | | - url: 'localhost', |
42 | | - port: 80 |
43 | | -}) |
44 | | -``` |
45 | | - |
46 | | -in case if you are using builders like `webpack` and `npm` then you need to import library at the top with: |
47 | | -```js |
48 | | -var ClusterWS = require('clusterws-client-js').ClusterWS |
49 | | -``` |
50 | | - |
51 | | -*All available options of ClusterWS:* |
52 | | -```js |
53 | | -{ |
54 | | - url: '{string} url of the server without http or https. (must be provided)', |
55 | | - port: '{number} port of the server. (must be provided)', |
56 | | - autoReconnect: '{boolean} allow to auto-reconnect to the server on lost connection. (default false)', |
57 | | - reconnectionIntervalMin: '{number} how long min time waut. (default 1000) in ms', |
58 | | - reconnectionIntervalMax: '{number} how long max time wait. (default 5000) in ms', |
59 | | - reconnectionAttempts: '{number} how many times to try, 0 means without limit. (default 0)', |
60 | | - secure: '{boolean} user secure connection or not wss/ws. (default false)' |
61 | | -} |
62 | | -``` |
63 | | - |
64 | | -*Auto reconnect count random time between Max and Min interval value this will reduce amount of users which are connection at the same time on reconnection and reduce server load on restart of the server* |
65 | | - |
66 | | -### 2. Listen on events |
67 | | -To listen on events from the server you should use `on` method witch is provided by `cws` |
68 | | -```js |
69 | | -/** |
70 | | - event name: string - can be any string you wish |
71 | | - data: any - is what you send from the client |
72 | | -*/ |
73 | | -cws.on('event name', function(data){ |
74 | | - // in here you can write any logic |
75 | | -}) |
76 | | -``` |
77 | | - |
78 | | -*Also `cws` gets **Reserved Events** such as `'connect'`, `'disconnect'` and `'error'`* |
79 | | -```js |
80 | | -cws.on('connect', function(){ |
81 | | - // in here you can write any logic |
82 | | -}) |
83 | | - |
84 | | -/** |
85 | | - err: any - display the problem with your weboscket |
86 | | -*/ |
87 | | -cws.on('error', function(err){ |
88 | | - // in here you can write any logic |
89 | | -}) |
90 | | - |
91 | | -/** |
92 | | - code: number - represent the reason in number |
93 | | - reason: string - reason why your socket was disconnected |
94 | | -*/ |
95 | | -cws.on('disconnect', function(code, reason){ |
96 | | - // in here you can write any logic |
97 | | -}) |
98 | | -``` |
99 | | - |
100 | | -### 3. Send events |
101 | | -To send events to the server use `send` method witch is provided by `cws` |
102 | | -```js |
103 | | -/** |
104 | | - event name: string - can be any string you wish (client must listen on this event name) |
105 | | - data: any - is what you want to send to the client |
106 | | -*/ |
107 | | -cws.send('event name', data) |
108 | | -``` |
109 | | - |
110 | | -*Avoid emitting **Reserved Events** such as `'connect'`, `'connection'`, `'disconnect'` and `'error'`. Also avoid emitting event and events with `'#'` at the start.* |
111 | | - |
112 | | -## Pub/Sub |
113 | | -You can `subscribe`, `watch`, `unsubscribe` and `publish`, `getChannelByName` to/from the channels |
114 | | -```js |
115 | | -/** |
116 | | - channel name: string - can be any string you wish |
117 | | -*/ |
118 | | -var channel = cws.subscribe('channel name') |
119 | | - |
120 | | -/** |
121 | | - data: any - is what you get when you or some one else publish to the channel |
122 | | -*/ |
123 | | -channel.watch(function(data){ |
124 | | - // in here you can write any logic |
125 | | -}) |
126 | | - |
127 | | -/** |
128 | | - data: any - is what you want to publish to the channel (everyone who is subscribe will get it) |
129 | | -*/ |
130 | | -channel.publish(data) |
131 | | - |
132 | | -/** |
133 | | - This method is used to unsubscribe from the channel |
134 | | -*/ |
135 | | -channel.unsubscribe() |
136 | | - |
137 | | -/** |
138 | | - Also you can chain everything in one expression |
139 | | -*/ |
140 | | -var channel = cws.subscribe('channel name').watch(function(data){ |
141 | | - // in here you can write any logic |
142 | | -}).publish(data) |
143 | | - |
144 | | - |
145 | | -/** |
146 | | - You can get channel by channel name only if you were subscribed before |
147 | | - You can use any methods as with usual channel |
148 | | -*/ |
149 | | -cws.getChannelByName('channel name') |
150 | | - |
151 | | -``` |
152 | | - |
153 | | -**To make sure that user is connected to the server before subscribing, do it on `connect` event or on any other events which you emit from the server, otherwise subscription may not work properly** |
154 | | - |
155 | | -## See Also |
156 | | -* [Medium ClusterWS](https://medium.com/clusterws) |
157 | | -* [ClusterWS Tests](https://github.com/ClusterWS/ClusterWS-Tests) |
158 | | -* [ClusterWS Example Chat](https://github.com/goriunov/ClusterWS-Chat-Example) |
159 | | - |
160 | | -*Docs are still under development. If you have found any errors please submit pull request or leave issue* |
| 13 | +<p align="center"> |
| 14 | + <i>Official JavaScript Client library for <a href="https://github.com/ClusterWS/ClusterWS">ClusterWS</a> - lightweight, fast and powerful framework for building horizontally & vertically scalable WebSocket applications in Node.js</i> |
| 15 | +</p> |
161 | 16 |
|
162 | | -## Happy coding !!! :sunglasses: |
| 17 | +<h1></h1> |
| 18 | +<h3 align="center"> |
| 19 | + <a href="https://github.com/ClusterWS/ClusterWS-Client-JS/wiki"><strong>ClusterWS JavaScript Client Documentation</strong></a> |
| 20 | +</h3> |
0 commit comments