Skip to main content
Version: 4.x

Client Initialization

Once you have installed the Socket.IO client library, you can now init the client. The complete list of options can be found here.

In the examples below, the io object comes either from:

  • the <script> import
<script src="/"></script>
  • NPM
// ES6 import or TypeScriptimport { io } from "";// CommonJSconst io = require("");

From the same domain#

If your front is served on the same domain as your server, you can simply use:

const socket = io();

The server URL will be deduced from the window.location object.

From a different domain#

In case your front is not served from the same domain as your server, you have to pass the URL of your server.

const socket = io("");

In that case, please make sure to enable Cross-Origin Resource Sharing (CORS) on the server.

Note: You can use either https or wss (respectively, http or ws).

// the following forms are similarconst socket = io("");const socket = io("wss://");const socket = io(""); // only in the browser when the page is served over https (will not work in Node.js)

Custom namespace#

In the examples above, the client will connect to the main namespace. Using only the main namespace should be sufficient for most use cases, but you can specify the namespace with:

// same origin versionconst socket = io("/admin");// cross origin versionconst socket = io("");

You can find more details about namespaces here.


The complete list of available options can be found here.