You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.5 KiB

8 years ago
  1. # socket.io-client
  2. [![Build Status](https://secure.travis-ci.org/socketio/socket.io-client.svg?branch=master)](http://travis-ci.org/socketio/socket.io-client)
  3. [![Dependency Status](https://david-dm.org/socketio/socket.io-client.svg)](https://david-dm.org/socketio/socket.io-client)
  4. [![devDependency Status](https://david-dm.org/socketio/socket.io-client/dev-status.svg)](https://david-dm.org/socketio/socket.io-client#info=devDependencies)
  5. ![NPM version](https://badge.fury.io/js/socket.io-client.svg)
  6. ![Downloads](http://img.shields.io/npm/dm/socket.io-client.svg?style=flat)
  7. [![](http://slack.socket.io/badge.svg?)](http://slack.socket.io)
  8. [![Sauce Test Status](https://saucelabs.com/browser-matrix/socket.svg)](https://saucelabs.com/u/socket)
  9. ## How to use
  10. A standalone build of `socket.io-client` is exposed automatically by the
  11. socket.io server as `/socket.io/socket.io.js`. Alternatively you can
  12. serve the file `socket.io.js` or `socket.io.min.js` found in the `dist` folder.
  13. ```html
  14. <script src="/socket.io/socket.io.js"></script>
  15. <script>
  16. var socket = io('http://localhost');
  17. socket.on('connect', function(){});
  18. socket.on('event', function(data){});
  19. socket.on('disconnect', function(){});
  20. </script>
  21. ```
  22. Socket.IO is compatible with [browserify](http://browserify.org/).
  23. ### Node.JS (server-side usage)
  24. Add `socket.io-client` to your `package.json` and then:
  25. ```js
  26. var socket = require('socket.io-client')('http://localhost');
  27. socket.on('connect', function(){});
  28. socket.on('event', function(data){});
  29. socket.on('disconnect', function(){});
  30. ```
  31. ## API
  32. ### IO(url:String, opts:Object):Socket
  33. Exposed as the `io` namespace in the standalone build, or the result
  34. of calling `require('socket.io-client')`.
  35. When called, it creates a new `Manager` for the given URL, and attempts
  36. to reuse an existing `Manager` for subsequent calls, unless the
  37. `multiplex` option is passed with `false`.
  38. The rest of the options are passed to the `Manager` constructor (see below
  39. for details).
  40. A `Socket` instance is returned for the namespace specified by the
  41. pathname in the URL, defaulting to `/`. For example, if the `url` is
  42. `http://localhost/users`, a transport connection will be established to
  43. `http://localhost` and a Socket.IO connection will be established to
  44. `/users`.
  45. ### IO#protocol
  46. Socket.io protocol revision number this client works with.
  47. ### IO#Socket
  48. Reference to the `Socket` constructor.
  49. ### IO#Manager
  50. Reference to the `Manager` constructor.
  51. ### IO#Emitter
  52. Reference to the `Emitter` constructor.
  53. ### Manager(url:String, opts:Object)
  54. A `Manager` represents a connection to a given Socket.IO server. One or
  55. more `Socket` instances are associated with the manager. The manager
  56. can be accessed through the `io` property of each `Socket` instance.
  57. The `opts` are also passed to `engine.io` upon initialization of the
  58. underlying `Socket`.
  59. Options:
  60. - `reconnection` whether to reconnect automatically (`true`)
  61. - `reconnectionAttempts` (`Infinity`) before giving up
  62. - `reconnectionDelay` how long to initially wait before attempting a new
  63. reconnection (`1000`). Affected by +/- `randomizationFactor`,
  64. for example the default initial delay will be between 500 to 1500ms.
  65. - `reconnectionDelayMax` maximum amount of time to wait between
  66. reconnections (`5000`). Each attempt increases the reconnection delay by 2x
  67. along with a randomization as above
  68. - `randomizationFactor` (`0.5`), 0 <= randomizationFactor <= 1
  69. - `timeout` connection timeout before a `connect_error`
  70. and `connect_timeout` events are emitted (`20000`)
  71. - `autoConnect` by setting this false, you have to call `manager.open`
  72. whenever you decide it's appropriate
  73. #### Events
  74. - `connect_error`. Fired upon a connection error.
  75. Parameters:
  76. - `Object` error object
  77. - `connect_timeout`. Fired upon a connection timeout.
  78. - `reconnect`. Fired upon a successful reconnection.
  79. Parameters:
  80. - `Number` reconnection attempt number
  81. - `reconnect_attempt`. Fired upon an attempt to reconnect.
  82. - `reconnecting`. Fired upon an attempt to reconnect.
  83. Parameters:
  84. - `Number` reconnection attempt number
  85. - `reconnect_error`. Fired upon a reconnection attempt error.
  86. Parameters:
  87. - `Object` error object
  88. - `reconnect_failed`. Fired when couldn't reconnect within `reconnectionAttempts`
  89. - `ping`. Fired when a ping packet is written out to the server.
  90. - `pong`. Fired when a pong is received from the server.
  91. Parameters:
  92. - `Number` number of ms elapsed since `ping` packet (i.e.: latency).
  93. The events above are also emitted on the individual sockets that
  94. reconnect that depend on this `Manager`.
  95. ### Manager#reconnection(v:Boolean):Manager
  96. Sets the `reconnection` option, or returns it if no parameters
  97. are passed.
  98. ### Manager#reconnectionAttempts(v:Boolean):Manager
  99. Sets the `reconnectionAttempts` option, or returns it if no parameters
  100. are passed.
  101. ### Manager#reconnectionDelay(v:Boolean):Manager
  102. Sets the `reconectionDelay` option, or returns it if no parameters
  103. are passed.
  104. ### Manager#reconnectionDelayMax(v:Boolean):Manager
  105. Sets the `reconectionDelayMax` option, or returns it if no parameters
  106. are passed.
  107. ### Manager#timeout(v:Boolean):Manager
  108. Sets the `timeout` option, or returns it if no parameters
  109. are passed.
  110. ### Socket
  111. #### Socket#id:String
  112. A property on the `socket` instance that is equal to the underlying engine.io socket id.
  113. The property is present once the socket has connected, is removed when the socket disconnects and is updated if the socket reconnects.
  114. #### Socket#compress(v:Boolean):Socket
  115. Sets a modifier for a subsequent event emission that the event data will
  116. only be _compressed_ if the value is `true`. Defaults to `true` when you don't call the method.
  117. ```js
  118. socket.compress(false).emit('an event', { some: 'data' });
  119. ```
  120. #### Events
  121. - `connect`. Fired upon a connection including a successful reconnection.
  122. - `error`. Fired upon a connection error
  123. Parameters:
  124. - `Object` error data
  125. - `disconnect`. Fired upon a disconnection.
  126. - `reconnect`. Fired upon a successful reconnection.
  127. Parameters:
  128. - `Number` reconnection attempt number
  129. - `reconnect_attempt`. Fired upon an attempt to reconnect.
  130. - `reconnecting`. Fired upon an attempt to reconnect.
  131. Parameters:
  132. - `Number` reconnection attempt number
  133. - `reconnect_error`. Fired upon a reconnection attempt error.
  134. Parameters:
  135. - `Object` error object
  136. - `reconnect_failed`. Fired when couldn't reconnect within `reconnectionAttempts`
  137. ## License
  138. [MIT](/LICENSE)