IO
Exposed as the io
namespace in the standalone build, or the result of calling require('socket.io-client')
.
|
|
io.protocol
- (Number)
The protocol revision number (currently: 4).
The protocol defines the format of the packets exchanged between the client and the server. Both the client and the server must use the same revision in order to understand each other.
You can find more information here.
io([url][, options])
url
(String) (defaults towindow.location
)options
(Object)forceNew
(Boolean) whether to reuse an existing connection
- Returns
Socket
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
. Passing this option is the equivalent of passing 'force new connection': true
or forceNew: true
.
A new 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
.
Query parameters can also be provided, either with the query
option or directly in the url (example: http://localhost/users?token=abc
).
|
is the short version of:
|
See new Manager(url[, options]) for the list of available options
.
Initialization examples
With multiplexing
By default, a single connection is used when connecting to different namespaces (to minimize resources):
|
That behaviour can be disabled with the forceNew
option:
|
Note: reusing the same namespace will also create two connections
|
With custom path
|
The request URLs will look like: localhost/myownpath/?EIO=3&transport=polling&sid=<id>
|
Here, the socket connects to the admin
namespace, with the custom path mypath
.
The request URLs will look like: localhost/mypath/?EIO=3&transport=polling&sid=<id>
(the namespace is sent as part of the payload).
With query parameters
|
With query option
|
The query content can also be updated on reconnection:
|
With extraHeaders
This only works if polling
transport is enabled (which is the default). Custom headers will not be appended when using websocket
as the transport. This happens because the WebSocket handshake does not honor custom headers. (For background see the WebSocket protocol RFC)
|
With websocket
transport only
By default, a long-polling connection is established first, then upgraded to “better” transports (like WebSocket). If you like to live dangerously, this part can be skipped:
|
With a custom parser
The default parser promotes compatibility (support for Blob
, File
, binary check) at the expense of performance. A custom parser can be provided to match the needs of your application. Please see the example here.
|
With a self-signed certificate
|