On this page
Server Initialization
Once you have installed the Socket.IO server library, you can now init the server. The complete list of options can be found here.
Syntax
CommonJS
|
ES modules
Please see here for enabling ECMAScript modules in your Node.js project.
|
TypeScript
First, you need to install the types: npm i --save-dev @types/socket.io
|
Initialization
Standalone
|
You can also pass the port as the first argument:
|
This implicitly starts a Node.js HTTP server, which can be accessed through io.httpServer.
Attached to an existing HTTP server
|
With HTTPS:
|
With Express
|
More information here.
With Koa
|
More information here.
Notable options
The complete list of options can be found here. Here are those which you will most likely use:
perMessageDeflate option
Default value: false
The WebSocket server provided by the ws package supports the permessage-deflate extension, which enables the client and server to negotiate a compression algorithm and its parameters, and then selectively apply it to the data payloads of each WebSocket message.
Starting from Socket.IO v2.4.0 (and in v3), this extension is now disabled by default, because it adds a significant overhead in terms of performance and memory consumption (and the ws maintainers suggest to only enable it if it is really needed).
For previous versions, you can disable it with:
|
maxHttpBufferSize option
Default value: 10e7
This defines how many bytes a message can be, before closing the socket. It defaults to 10e7 (100MB). You may increase or decrement this value depending on your needs.
|
It matches the maxPayload option of the ws package.
© 2014–2020 Automattic
Licensed under the MIT License.
https://socket.io/docs/v2/server-initialization