Installation

Compatibility(兼容性)

Socket.IO does support IE9 and above. IE 6/7/8 are not supported anymore.

Browser compatibility is tested thanks to the awesome Sauce Labs platform:

Socket.IO确实支持IE9及更高版本。 不再支持IE 6/7/8。

出色的Sauce Labs平台对浏览器的兼容性进行了测试:

Browser support

Release notes

The release notes of each version can be found in GitHub.

Installation

Standalone build(独立构建)

By default, the Socket.IO server exposes a client bundle at /socket.io/socket.io.js.

io will be registered as a global variable:

默认情况下,Socket.IO服务器在/socket.io/socket.io.js公开客户端捆绑包。

io将被注册为全局变量:

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

If you don’t need this (see other options below), you can disable the functionality on the server side:

如果不需要此功能(请参见下面的其他选项),则可以在服务器端禁用此功能:

1
2
3
const io = require('socket.io')({
serveClient: false
});

From a CDN

You can also include the client bundle from a CDN:

1
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>

Socket.IO is also available from other CDN like jsDelivr and unpkg.

There are several bundles available:

也可以从其他CDN(如jsDelivr和unpkg)获得Socket.IO。

有几种可用的捆绑包:

Name Size Description
socket.io.js 19.8 kB min+gzip Production version, with debug
socket.io.slim.js 15.6 kB min+gzip Production version, without debug
socket.io.dev.js 38.5 kB gzip Unminified version, with debug

The debug package allows to print debug information to the console. You can find more information here.

During development, we recommend to use the socket.io.dev.js bundle. By setting localStorage.debug = 'socket.io-client:socket', any event received by the client will be printed to the console.

For production, please use the socket.io.slim.js bundle, which is an optimized build excluding the debug package.

[debug](https://www.npmjs.com/package/debug)软件包可将调试信息打印到控制台。 您可以在[此处](https://socket.io/docs/logging-and-debugging/)中找到更多信息。

在开发期间,我们建议使用socket.io.dev.js捆绑包。 通过设置localStorage.debug ='socket.io-client:socket',客户端收到的任何事件都将被打印到控制台。

对于生产,请使用socket.io.slim.js捆绑包,该捆绑包是经过优化的构建,不包括调试包。

From NPM

The Socket.IO client is compatible with bundler like webpack or browserify.

Socket.IO客户端与webpack或browserify等捆绑程序兼容。

1
npm install socket.io-client

The client can also be run from Node.js.

Caught a mistake? Edit this page on GitHub