客户端API

IO

The io method is bound to the global scope in the standalone build:

  1. <script src="/socket.io/socket.io.js"></script>
  2. <script>
  3. const socket = io();
  4. </script>

An ESM bundle is also available since version 4.3.0:

  1. <script type="module">
  2. import { io } from "https://cdn.socket.io/4.7.5/socket.io.esm.min.js";
  3. const socket = io();
  4. </script>

With an import map:

  1. <script type="importmap">
  2. {
  3. "imports": {
  4. "socket.io-client": "https://cdn.socket.io/4.7.5/socket.io.esm.min.js"
  5. }
  6. }
  7. </script>
  8. <script type="module">
  9. import { io } from "socket.io-client";
  10. const socket = io();
  11. </script>

Else, in all other cases (with some build tools, in Node.js or React Native), it can be imported from the socket.io-client package:

  1. // ES modules
  2. import { io } from "socket.io-client";
  3. // CommonJS
  4. const { io } = require("socket.io-client");

io.protocol

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])