IO
Exposed as the io
namespace in the standalone build, or the result of calling require("socket.io-client")
.
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io("http://localhost");
</script>
const io = require("socket.io-client");
// or with import syntax
import { io } from "socket.io-client";
io.protocol
- (Number)
The protocol revision number (currently: 5).
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
).
const io = require("socket.io-client");
const socket = io("ws://example.com/my-namespace", {
reconnectionDelayMax: 10000,
auth: {
token: "123"
},
query: {
"my-key": "my-value"
}
});
is the short version of:
const { Manager } = require("socket.io-client");
const manager = new Manager("ws://example.com", {
reconnectionDelayMax: 10000,
query: {
"my-key": "my-value"
}
});
const socket = manager.socket("/my-namespace", {
auth: {
token: "123"
}
});
See new Manager(url[, options]) for the list of available options
.
Please note: manager.socket("/my-namespace", options )
will only read the auth
key in the options
object.query: {…}
and other optional values are only used when passed via a new Manager(uri, options)
instance.
See Migrating from 2.x to 3.0 for more on the difference between the auth
and query
options.