4 #### Sockets for the rest of us
6 The `socket.io` client is basically a simple HTTP Socket interface implementation.
7 It looks similar to WebSocket while providing additional features and
8 leveraging other transports when WebSocket is not supported by the user's
12 var socket = io.connect('http://domain.com');
13 socket.on('connect', function () {
16 socket.on('custom event', function () {
17 // server emitted a custom event
19 socket.on('disconnect', function () {
20 // socket disconnected
22 socket.send('hi there');
27 #### Utilizing namespaces (ie: multiple sockets)
29 If you want to namespace all the messages and events emitted to a particular
30 endpoint, simply specify it as part of the `connect` uri:
33 var chat = io.connect('http://localhost/chat');
34 chat.on('connect', function () {
35 // chat socket connected
38 var news = io.connect('/news'); // io.connect auto-detects host
39 news.on('connect', function () {
40 // news socket connected
44 #### Emitting custom events
46 To ease with the creation of applications, you can emit custom events outside
47 of the global `message` event.
50 var socket = io.connect();
51 socket.emit('server custom event', { my: 'data' });
54 #### Forcing disconnection
57 var socket = io.connect();
58 socket.on('connect', function () {
68 io.connect(uri, [options]);
77 The resource is what allows the `socket.io` server to identify incoming connections by `socket.io` clients. In other words, any HTTP server can implement socket.io and still serve other normal, non-realtime HTTP requests.
82 ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']
85 A list of the transports to attempt to utilize (in order of preference).
93 The amount of milliseconds a transport has to create a connection before we consider it timed out.
95 - *'try multiple transports'*
101 A boolean indicating if we should try other transports when the connectTimeout occurs.
109 A boolean indicating if we should automatically reconnect if a connection is disconnected.
111 - *'reconnection delay'*
117 The amount of milliseconds before we try to connect to the server again. We are using a exponential back off algorithm for the following reconnections, on each reconnect attempt this value will get multiplied (500 > 1000 > 2000 > 4000 > 8000).
120 - *'max reconnection attempts'*
126 The amount of attempts should we make using the current transport to connect to the server? After this we will do one final attempt, and re-try with all enabled transport methods before we give up.
132 The passed in options combined with the defaults.
136 Whether the socket is connected or not.
140 Whether the socket is connecting or not.
144 Whether we are reconnecting or not.
148 The transport instance.
154 Establishes a connection. If λ is supplied as argument, it will be called once the connection is established.
158 A string of data to send.
162 Closes the connection.
166 Adds a listener for the event *event*.
170 Adds a one time listener for the event *event*. The listener is removed after the first time the event is fired.
172 - *removeListener(event, λ)*
174 Removes the listener λ for the event *event*.
180 Fired when the connection is established and the handshake successful.
182 - *connecting(transport_type)*
184 Fired when a connection is attempted, passing the transport name.
188 Fired when the connection timeout occurs after the last connection attempt.
189 This only fires if the `connectTimeout` option is set.
190 If the `tryTransportsOnConnectTimeout` option is set, this only fires once all
191 possible transports have been tried.
195 Fired when a message arrives from the server
199 Fired when the connection is closed. Be careful with using this event, as some transports will fire it even under temporary, expected disconnections (such as XHR-Polling).
203 Fired when the connection is considered disconnected.
205 - *reconnect(transport_type,reconnectionAttempts)*
207 Fired when the connection has been re-established. This only fires if the `reconnect` option is set.
209 - *reconnecting(reconnectionDelay,reconnectionAttempts)*
211 Fired when a reconnection is attempted, passing the next delay for the next reconnection.
215 Fired when all reconnection attempts have failed and we where unsuccessful in reconnecting to the server.
219 Guillermo Rauch <guillermo@learnboost.com>
221 Arnout Kazemier <info@3rd-eden.com>
227 Copyright (c) 2010 LearnBoost <dev@learnboost.com>
229 Permission is hereby granted, free of charge, to any person obtaining
230 a copy of this software and associated documentation files (the
231 'Software'), to deal in the Software without restriction, including
232 without limitation the rights to use, copy, modify, merge, publish,
233 distribute, sublicense, and/or sell copies of the Software, and to
234 permit persons to whom the Software is furnished to do so, subject to
235 the following conditions:
237 The above copyright notice and this permission notice shall be
238 included in all copies or substantial portions of the Software.
240 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
241 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
242 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
243 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
244 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
245 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
246 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.