NewsBlur/node/node_modules/socket.io-client/README.md

191 lines
6.5 KiB
Markdown
Raw Normal View History

2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
# socket.io-client
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
[![Build Status](https://secure.travis-ci.org/socketio/socket.io-client.svg?branch=master)](http://travis-ci.org/socketio/socket.io-client)
[![Dependency Status](https://david-dm.org/socketio/socket.io-client.svg)](https://david-dm.org/socketio/socket.io-client)
[![devDependency Status](https://david-dm.org/socketio/socket.io-client/dev-status.svg)](https://david-dm.org/socketio/socket.io-client#info=devDependencies)
![NPM version](https://badge.fury.io/js/socket.io-client.svg)
![Downloads](http://img.shields.io/npm/dm/socket.io-client.svg?style=flat)
[![](http://slack.socket.io/badge.svg?)](http://slack.socket.io)
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
[![Sauce Test Status](https://saucelabs.com/browser-matrix/socket.svg)](https://saucelabs.com/u/socket)
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
## How to use
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
A standalone build of `socket.io-client` is exposed automatically by the
socket.io server as `/socket.io/socket.io.js`. Alternatively you can
serve the file `socket.io.js` or `socket.io.min.js` found in the `dist` folder.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
```html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost');
socket.on('connect', function(){});
socket.on('event', function(data){});
socket.on('disconnect', function(){});
</script>
2012-01-02 18:24:53 -08:00
```
2016-11-29 18:29:50 -08:00
Socket.IO is compatible with [browserify](http://browserify.org/).
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### Node.JS (server-side usage)
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Add `socket.io-client` to your `package.json` and then:
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
```js
var socket = require('socket.io-client')('http://localhost');
socket.on('connect', function(){});
socket.on('event', function(data){});
socket.on('disconnect', function(){});
```
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
## API
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### IO(url:String, opts:Object):Socket
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Exposed as the `io` namespace in the standalone build, or the result
of calling `require('socket.io-client')`.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
When called, it creates a new `Manager` for the given URL, and attempts
to reuse an existing `Manager` for subsequent calls, unless the
`multiplex` option is passed with `false`.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
The rest of the options are passed to the `Manager` constructor (see below
for details).
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
A `Socket` instance is returned for the namespace specified by the
pathname in the URL, defaulting to `/`. For example, if the `url` is
`http://localhost/users`, a transport connection will be established to
`http://localhost` and a Socket.IO connection will be established to
`/users`.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### IO#protocol
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Socket.io protocol revision number this client works with.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### IO#Socket
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Reference to the `Socket` constructor.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### IO#Manager
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Reference to the `Manager` constructor.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### IO#Emitter
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Reference to the `Emitter` constructor.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### Manager(url:String, opts:Object)
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
A `Manager` represents a connection to a given Socket.IO server. One or
more `Socket` instances are associated with the manager. The manager
can be accessed through the `io` property of each `Socket` instance.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
The `opts` are also passed to `engine.io` upon initialization of the
underlying `Socket`.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Options:
- `reconnection` whether to reconnect automatically (`true`)
- `reconnectionAttempts` (`Infinity`) before giving up
- `reconnectionDelay` how long to initially wait before attempting a new
reconnection (`1000`). Affected by +/- `randomizationFactor`,
for example the default initial delay will be between 500 to 1500ms.
- `reconnectionDelayMax` maximum amount of time to wait between
reconnections (`5000`). Each attempt increases the reconnection delay by 2x
along with a randomization as above
- `randomizationFactor` (`0.5`), 0 <= randomizationFactor <= 1
- `timeout` connection timeout before a `connect_error`
and `connect_timeout` events are emitted (`20000`)
- `autoConnect` by setting this false, you have to call `manager.open`
whenever you decide it's appropriate
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
#### Events
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
- `connect_error`. Fired upon a connection error.
Parameters:
- `Object` error object
- `connect_timeout`. Fired upon a connection timeout.
- `reconnect`. Fired upon a successful reconnection.
Parameters:
- `Number` reconnection attempt number
- `reconnect_attempt`. Fired upon an attempt to reconnect.
- `reconnecting`. Fired upon an attempt to reconnect.
Parameters:
- `Number` reconnection attempt number
- `reconnect_error`. Fired upon a reconnection attempt error.
Parameters:
- `Object` error object
- `reconnect_failed`. Fired when couldn't reconnect within `reconnectionAttempts`
- `ping`. Fired when a ping packet is written out to the server.
- `pong`. Fired when a pong is received from the server.
Parameters:
- `Number` number of ms elapsed since `ping` packet (i.e.: latency).
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
The events above are also emitted on the individual sockets that
reconnect that depend on this `Manager`.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### Manager#reconnection(v:Boolean):Manager
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Sets the `reconnection` option, or returns it if no parameters
are passed.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### Manager#reconnectionAttempts(v:Boolean):Manager
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Sets the `reconnectionAttempts` option, or returns it if no parameters
are passed.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### Manager#reconnectionDelay(v:Boolean):Manager
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Sets the `reconectionDelay` option, or returns it if no parameters
are passed.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### Manager#reconnectionDelayMax(v:Boolean):Manager
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Sets the `reconectionDelayMax` option, or returns it if no parameters
are passed.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### Manager#timeout(v:Boolean):Manager
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Sets the `timeout` option, or returns it if no parameters
are passed.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
### Socket
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
#### Socket#id:String
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
A property on the `socket` instance that is equal to the underlying engine.io socket id.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
The property is present once the socket has connected, is removed when the socket disconnects and is updated if the socket reconnects.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
#### Socket#compress(v:Boolean):Socket
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
Sets a modifier for a subsequent event emission that the event data will
only be _compressed_ if the value is `true`. Defaults to `true` when you don't call the method.
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
```js
socket.compress(false).emit('an event', { some: 'data' });
```
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
#### Events
2012-01-02 18:24:53 -08:00
2016-11-29 18:29:50 -08:00
- `connect`. Fired upon a connection including a successful reconnection.
- `error`. Fired upon a connection error
Parameters:
- `Object` error data
- `disconnect`. Fired upon a disconnection.
- `reconnect`. Fired upon a successful reconnection.
Parameters:
- `Number` reconnection attempt number
- `reconnect_attempt`. Fired upon an attempt to reconnect.
- `reconnecting`. Fired upon an attempt to reconnect.
Parameters:
- `Number` reconnection attempt number
- `reconnect_error`. Fired upon a reconnection attempt error.
Parameters:
- `Object` error object
- `reconnect_failed`. Fired when couldn't reconnect within `reconnectionAttempts`
## License
[MIT](/LICENSE)