UNPKG

5.02 kBMarkdownView Raw
1SocketCluster JavaScript Client
2======
3
4SocketCluster Client is the client-side component of SocketCluster.
5
6## Usage Overview
7
8* [How to use](https://github.com/SocketCluster/socketcluster-client#how-to-use)
9* [Connect Options](https://github.com/SocketCluster/socketcluster-client#connect-options)
10* [Events](https://github.com/SocketCluster/socketcluster-client#events)
11* [Developing](https://github.com/SocketCluster/socketcluster-client#developing)
12
13To install, run:
14
15```bash
16npm install socketcluster-client
17```
18
19
20## How to use
21
22The socketcluster-client script is called `socketcluster.js` (located in the main socketcluster-client directory).
23Embed it in your HTML page like that:
24
25```html
26<script type="text/javascript" src="/socketcluster.js"></script>
27```
28- Note that the src attribute may be different depending on how you setup your HTTP server
29
30Once you have embedded the client socketcluster.js into your page, you will gain access to a global socketCluster object.
31Then, to begin interacting with the SocketCluster cluster, you will need to establish a connection.
32Once that's done, you will be able to emit events to the server and listen to incoming events (example code):
33
34```js
35var options = {
36 port: 8000
37};
38
39// Initiate the connection to the server
40var socket = socketCluster.create(options);
41
42socket.on('connect', function () {
43 console.log('CONNECTED');
44});
45
46// Listen to an event called 'rand' from the server
47socket.on('rand', function (num) {
48 console.log('RANDOM: ' + num);
49 var curHTML = document.body.innerHTML;
50 curHTML += 'RANDOM: ' + num + '<br />';
51 document.body.innerHTML = curHTML;
52});
53```
54
55Example with HTTPS:
56
57```js
58var options = {
59 hostname: 'securedomain.com',
60 secure: true,
61 port: 443,
62 rejectUnauthorized: false // Only necessary during debug if using a self-signed certificate
63};
64// Initiate the connection to the server
65var socket = socketCluster.create(options);
66```
67
68## Connect Options
69
70See all available options : https://socketcluster.io/#!/docs/api-socketcluster-client
71
72```js
73var options = {
74 path: '/socketcluster/',
75 port: 8000,
76 hostname: '127.0.0.1',
77 autoConnect: true,
78 secure: false,
79 rejectUnauthorized: false,
80 connectTimeout: 10000, //milliseconds
81 ackTimeout: 10000, //milliseconds
82 channelPrefix: null,
83 disconnectOnUnload: true,
84 multiplex: true,
85 autoReconnectOptions: {
86 initialDelay: 10000, //milliseconds
87 randomness: 10000, //milliseconds
88 multiplier: 1.5, //decimal
89 maxDelay: 60000 //milliseconds
90 },
91 authEngine: null,
92 codecEngine: null,
93 subscriptionRetryOptions: {},
94 query: {
95 yourparam: 'hello'
96 }
97};
98```
99
100## Events
101
102```js
103socket.on('subscribe', function(channelname) {
104 console.log('subscribe:' + channelname);
105});
106
107socket.on('subscribeFail', function(channelname) {
108 console.log('subscribeFail:' + channelname);
109});
110
111socket.on('unsubscribe', function(channelname) {
112 console.log('unsubscribe:' + channelname);
113});
114
115socket.on('subscribeStateChange', function(data) {
116 console.log('subscribeStateChange:' + JSON.stringify(data));
117});
118
119socket.on('message', function(data) {
120 console.log('message:' + data);
121});
122```
123
124## Developing
125
126#### Install all dependencies
127
128```bash
129cd socketcluster-client
130
131npm install -g gulp gulp-cli browserify uglify-js
132
133npm install
134```
135
136#### Building
137
138#### Via Browserify
139
140To build SocketCluster Client with browserify, use:
141
142```bash
143./browserify-build.sh
144```
145
146Or use
147
148```bash
149browserify -s socketCluster index.js > socketcluster.js && uglifyjs socketcluster.js -o socketcluster.min.js
150```
151
152#### Via Gulp
153
154To build SocketCluster Client with Gulp, use:
155
156```bash
157./gulp-build.sh
158```
159
160## Change log
161
162See the 'releases' section for changes: https://github.com/SocketCluster/socketcluster-client/releases
163
164## License
165
166(The MIT License)
167
168Copyright (c) 2013-2017 SocketCluster.io
169
170Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
171
172The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
173
174THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.