<commit_before>title: Server API
permalink: /docs/server-api/
type: api
---

# Server

Exposed by `require('socket.io')`.

## new Server(httpServer[, options])

  - `httpServer` _(http.Server)_ the server to bind to.
  - `options` _(Object)_

Works with and without `new`:

```js
const io = require('socket.io')();
// or
const Server = require('socket.io');
const io = new Server();
```

Available options:

Option | Default value | Description
------ | ------------- | -----------
`path` | `/socket.io` | name of the path to capture
`serveClient` | `true` | whether to serve the client files
`adapter` | - | the adapter to use. Defaults to an instance of the `Adapter` that ships with socket.io which is memory based. See [socket.io-adapter](https://github.com/socketio/socket.io-adapter)
`origins` | `*` | the allowed origins
`parser` | - | the parser to use. Defaults to an instance of the `Parser` that ships with socket.io. See [socket.io-parser](https://github.com/socketio/socket.io-parser).

Available options for the underlying Engine.IO server:

Option | Default value | Description
------ | ------------- | -----------
`pingTimeout` | `5000` | how many ms without a pong packet to consider the connection closed
`pingInterval` | `25000` | how many ms before sending a new ping packet
`upgradeTimeout` | `10000` | how many ms before an uncompleted transport upgrade is cancelled
`maxHttpBufferSize` | `10e7` | how many bytes or characters a message can be, before closing the session (to avoid DoS).
`allowRequest` | | A function that receives a given handshake or upgrade request as its first parameter, and can decide whether to continue or not. The second argument is a function that needs to be called with the decided information: `fn(err, success)`, where `success` is a boolean value where false means that the request is rejected, and err is an error code.
`transports` | `['polling', 'websocket']` | transports to allow connections to
`allowUpgrades` | `true` | whether to allow transport upgrades
`perMessageDeflate` | `true` | parameters of the WebSocket permessage-deflate extension (see [ws module](https://github.com/einaros/ws) api docs). Set to `false` to disable.
`httpCompression` | `true` | parameters of the http compression for the polling transports (see [zlib](http://nodejs.org/api/zlib.html#zlib_options) api docs). Set to `false` to disable.
`cookie` | `io` | name of the HTTP cookie that contains the client sid to send as part of handshake response headers. Set to `false` to not send one.
`cookiePath` | `/` | path of the above `cookie` option. If false, no path will be sent, which means browsers will only send the cookie on the engine.io attached path (`/engine.io`). Set false to not save io cookie on all requests.
`cookieHttpOnly` | `true` | if `true` HttpOnly io cookie cannot be accessed by client-side APIs, such as JavaScript. _This option has no effect if `cookie` or `cookiePath` is set to `false`._
`wsEngine` | `ws` | what WebSocket server implementation to use. Specified module must conform to the `ws` interface (see [ws module api docs](https://github.com/websockets/ws/blob/master/doc/ws.md)). Default value is `ws`. An alternative c++ addon is also available by installing `uws` module.

Among those options:

- The `pingTimeout` and `pingInterval` parameters will impact the delay before a client knows the server is not available anymore. For example, if the underlying TCP connection is not closed properly due to a network issue, a client may have to wait up to `pingTimeout + pingInterval` ms before getting a `disconnect` event.

- The order of the `transports` array is important. By default, a long-polling connection is established first, and then upgraded to WebSocket if possible. Using `['websocket']` means there will be no fallback if a WebSocket connection cannot be opened.

```js
const server = require('http').createServer();
<commit_msg>docs: add reference to eiows in the server API<commit_after>title: Server API
permalink: /docs/server-api/
type: api
---

# Server

Exposed by `require('socket.io')`.

## new Server(httpServer[, options])

  - `httpServer` _(http.Server)_ the server to bind to.
  - `options` _(Object)_

Works with and without `new`:

```js
const io = require('socket.io')();
// or
const Server = require('socket.io');
const io = new Server();
```

Available options:

Option | Default value | Description
------ | ------------- | -----------
`path` | `/socket.io` | name of the path to capture
`serveClient` | `true` | whether to serve the client files
`adapter` | - | the adapter to use. Defaults to an instance of the `Adapter` that ships with socket.io which is memory based. See [socket.io-adapter](https://github.com/socketio/socket.io-adapter)
`origins` | `*` | the allowed origins
`parser` | - | the parser to use. Defaults to an instance of the `Parser` that ships with socket.io. See [socket.io-parser](https://github.com/socketio/socket.io-parser).

Available options for the underlying Engine.IO server:

Option | Default value | Description
------ | ------------- | -----------
`pingTimeout` | `5000` | how many ms without a pong packet to consider the connection closed
`pingInterval` | `25000` | how many ms before sending a new ping packet
`upgradeTimeout` | `10000` | how many ms before an uncompleted transport upgrade is cancelled
`maxHttpBufferSize` | `10e7` | how many bytes or characters a message can be, before closing the session (to avoid DoS).
`allowRequest` | | A function that receives a given handshake or upgrade request as its first parameter, and can decide whether to continue or not. The second argument is a function that needs to be called with the decided information: `fn(err, success)`, where `success` is a boolean value where false means that the request is rejected, and err is an error code.
`transports` | `['polling', 'websocket']` | transports to allow connections to
`allowUpgrades` | `true` | whether to allow transport upgrades
`perMessageDeflate` | `true` | parameters of the WebSocket permessage-deflate extension (see [ws module](https://github.com/einaros/ws) api docs). Set to `false` to disable.
`httpCompression` | `true` | parameters of the http compression for the polling transports (see [zlib](http://nodejs.org/api/zlib.html#zlib_options) api docs). Set to `false` to disable.
`cookie` | `io` | name of the HTTP cookie that contains the client sid to send as part of handshake response headers. Set to `false` to not send one.
`cookiePath` | `/` | path of the above `cookie` option. If false, no path will be sent, which means browsers will only send the cookie on the engine.io attached path (`/engine.io`). Set false to not save io cookie on all requests.
`cookieHttpOnly` | `true` | if `true` HttpOnly io cookie cannot be accessed by client-side APIs, such as JavaScript. _This option has no effect if `cookie` or `cookiePath` is set to `false`._
`wsEngine` | `ws` | what WebSocket server implementation to use. Specified module must conform to the `ws` interface (see [ws module api docs](https://github.com/websockets/ws/blob/master/doc/ws.md)). Default value is `ws`. An alternative c++ addon is also available by installing the [eiows](https://www.npmjs.com/package/eiows) module.

Among those options:

- The `pingTimeout` and `pingInterval` parameters will impact the delay before a client knows the server is not available anymore. For example, if the underlying TCP connection is not closed properly due to a network issue, a client may have to wait up to `pingTimeout + pingInterval` ms before getting a `disconnect` event.

- The order of the `transports` array is important. By default, a long-polling connection is established first, and then upgraded to WebSocket if possible. Using `['websocket']` means there will be no fallback if a WebSocket connection cannot be opened.

```js
const server = require('http').createServer();

{'fraction_non_alphanumeric': 0.11510245383253226, 'fraction_numerical': 0.0043005312420946115, 'mean_word_length': 4.161227154046998, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 9, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2852070', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2122, 'n_words': 1035}