This version is a slightly modified version of the well-known `socket-io-client` in order to add React Native compatibility, including usage with real WebSockets as well as a fallback to long polling.
npm install react-native-socket.io-clientThis version is a slightly modified version of the well-known socket-io-client in order to add React Native compatibility, including usage with real WebSockets as well as a fallback to long polling.
This repo will follow the normal socket.io client release cycle and will disappear as an underlying issue with the React Native packager disappears.
A standalone build of react-native-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 found at the root of this repository.
``html`
Socket.IO is compatible with browserify.
Exposed as the io namespace in the standalone build, or the resultrequire('socket.io-client')
of calling .
When called, it creates a new Manager for the given URL, and attemptsManager
to reuse an existing for subsequent calls, unless themultiplex
option is passed with false.
The rest of the options are passed to the Manager constructor (see below
for details).
A Socket instance is returned for the namespace specified by the/
pathname in the URL, defaulting to . For example, if the url ishttp://localhost/users
, a transport connection will be established tohttp://localhost
and a Socket.IO connection will be established to/users
.
Socket.io protocol revision number this client works with.
Reference to the Socket constructor.
Reference to the Manager constructor.
Reference to the Emitter constructor.
A Manager represents a connection to a given Socket.IO server. One orSocket
more instances are associated with the manager. The managerio
can be accessed through the property of each Socket instance.
The opts are also passed to engine.io upon initialization of theSocket
underlying .
Options:
- reconnection whether to reconnect automatically (true)reconnectionAttempts
- (Infinity) before giving upreconnectionDelay
- how long to initially wait before attempting a new1000
reconnection (). Affected by +/- randomizationFactor,reconnectionDelayMax
for example the default initial delay will be between 500 to 1500ms.
- maximum amount of time to wait between5000
reconnections (). Each attempt increases the reconnection delay by 2xrandomizationFactor
along with a randomization as above
- (0.5), 0 <= randomizationFactor <= 1timeout
- connection timeout before a connect_errorconnect_timeout
and events are emitted (20000)autoConnect
- by setting this false, you have to call manager.open
whenever you decide it's appropriate
#### Events
- connect_error. Fired upon a connection error.Object
Parameters:
- error objectconnect_timeout
- . Fired upon a connection timeout.reconnect
- . Fired upon a successful reconnection.Number
Parameters:
- reconnection attempt numberreconnect_attempt
- . Fired upon an attempt to reconnect.reconnecting
- . Fired upon an attempt to reconnect.Number
Parameters:
- reconnection attempt numberreconnect_error
- . Fired upon a reconnection attempt error.Object
Parameters:
- error objectreconnect_failed
- . Fired when couldn't reconnect within reconnectionAttemptsping
- . Fired when a ping packet is written out to the server.pong
- . Fired when a pong is received from the server.Number
Parameters:
- number of ms elapsed since ping packet (i.e.: latency).
The events above are also emitted on the individual sockets that
reconnect that depend on this Manager.
Sets the reconnection option, or returns it if no parameters
are passed.
Sets the reconnectionAttempts option, or returns it if no parameters
are passed.
Sets the reconectionDelay option, or returns it if no parameters
are passed.
Sets the reconectionDelayMax option, or returns it if no parameters
are passed.
Sets the timeout option, or returns it if no parameters
are passed.
#### Socket#id:String
A property on the socket instance that is equal to the underlying engine.io socket id.
The property is present once the socket has connected, is removed when the socket disconnects and is updated if the socket reconnects.
#### Socket#compress(v:Boolean):Socket
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.
`js`
socket.compress(false).emit('an event', { some: 'data' });
#### Events
- connect. Fired upon a connection including a successful reconnection.error
- . Fired upon a connection errorObject
Parameters:
- error datadisconnect
- . Fired upon a disconnection.reconnect
- . Fired upon a successful reconnection.Number
Parameters:
- reconnection attempt numberreconnect_attempt
- . Fired upon an attempt to reconnect.reconnecting
- . Fired upon an attempt to reconnect.Number
Parameters:
- reconnection attempt numberreconnect_error
- . Fired upon a reconnection attempt error.Object
Parameters:
- error objectreconnect_failed
- . Fired when couldn't reconnect within reconnectionAttempts`