<commit_before>title: The Socket instance (server-side)
short_title: The Socket instance
permalink: /docs/v4/server-socket-instance/
release: v4
type: docs
order: 204
---

Besides:

- [emitting](/docs/v4/emitting-events/#Basic-emit) and [listening to](/docs/v4/listening-to-events/) events
- [broadcasting events](/docs/v4/broadcasting-events/#To-all-connected-clients-excepting-the-sender)
- [joining and leaving rooms](/docs/v4/rooms/#Joining-and-leaving)

The Socket instance has a few attributes that may be of use in your application:

## Socket#id

Each new connection is assigned a random 20-characters identifier.

This identifier is synced with the value on the client-side.

```js
// server-side
io.on("connection", (socket) => {
  console.log(socket.id); // ojIckSD2jqNzOqIrAGzL
});

// client-side
socket.on("connect", () => {
  console.log(socket.id); // ojIckSD2jqNzOqIrAGzL
});
```

Upon creation, the Socket joins the room identified by its own id, which means you can use it for private messaging:

```js
io.on("connection", socket => {
  socket.on("private message", (anotherSocketId, msg) => {
    socket.to(anotherSocketId).emit("private message", socket.id, msg);
  });
});
```

Note: you can't overwrite this identifier, as it is used in several parts of the Socket.IO codebase.

## Socket#handshake

This object contains some details about the handshake that happens at the beginning of the Socket.IO session.

```
{
  headers: /* the headers of the initial request */
  query: /* the query params of the initial request */
  auth: /* the authentication payload */
  time: /* the date of creation (as string) */
  issued: /* the date of creation (unix timestamp) */
  url: /* the request URL string */
  address: /* the ip of the client */
  xdomain: /* whether the connection is cross-domain */
  secure: /* whether the connection is secure */
}
```

Example:

```json
{
  "headers": {
    "user-agent": "xxxx",
    "accept": "*/*",
    "host": "example.com",
    "connection": "close"
  },
  "query": {
    "EIO": "4",
    "transport": "polling",
    "t": "NNjNltH"
  },
  "auth": {
    "token": "<PASSWORD>"
  },
  "time": "Sun Nov 22 2020 01:33:46 GMT+0100 (Central European Standard Time)",
  "issued": 1606005226969,
  "url": "/socket.io/?EIO=4&transport=polling&t=NNjNltH",
  "address": "::ffff:1.2.3.4",
  "xdomain": false,
  "secure": true
}
```

## Socket#rooms

This is a reference to the [rooms](/docs/v4/rooms/) the Socket is currently in.

```js
io.on("connection", (socket) => {
  console.log(socket.rooms); // Set { <socket.id> }
  socket.join("room1");
  console.log(socket.rooms); // Set { <socket.id>, "room1" }
});
```

## Socket#data

An arbitrary object that can be used in conjunction with the `fetchSockets()` utility method:

```js
// server A
io.on("connection", (socket) => {
  socket.data.username = "alice";
});

// server B
const sockets = await io.fetchSockets();
console.log(sockets[0].data.username); // "alice"
```

More information [here](/docs/v4/server-instance/#Utility-methods).

## Additional attributes

As long as you do not overwrite any existing attribute, you can attach any attribute to the Socket instance and use it later:

```js
// in a middleware
io.use(async (socket, next) => {
  try {
    const user = await fetchUser(socket);
    socket.user = user;
  } catch (e) {
    next(new Error("unknown user"));
  }
});

io.on("connection", (socket) => {
  console.log(socket.user);

  // in a listener
  socket.on("set username", (username) => {
    socket.username = username;<commit_msg>docs: add missing next() call in middleware (#286)<commit_after>title: The Socket instance (server-side)
short_title: The Socket instance
permalink: /docs/v4/server-socket-instance/
release: v4
type: docs
order: 204
---

Besides:

- [emitting](/docs/v4/emitting-events/#Basic-emit) and [listening to](/docs/v4/listening-to-events/) events
- [broadcasting events](/docs/v4/broadcasting-events/#To-all-connected-clients-excepting-the-sender)
- [joining and leaving rooms](/docs/v4/rooms/#Joining-and-leaving)

The Socket instance has a few attributes that may be of use in your application:

## Socket#id

Each new connection is assigned a random 20-characters identifier.

This identifier is synced with the value on the client-side.

```js
// server-side
io.on("connection", (socket) => {
  console.log(socket.id); // ojIckSD2jqNzOqIrAGzL
});

// client-side
socket.on("connect", () => {
  console.log(socket.id); // ojIckSD2jqNzOqIrAGzL
});
```

Upon creation, the Socket joins the room identified by its own id, which means you can use it for private messaging:

```js
io.on("connection", socket => {
  socket.on("private message", (anotherSocketId, msg) => {
    socket.to(anotherSocketId).emit("private message", socket.id, msg);
  });
});
```

Note: you can't overwrite this identifier, as it is used in several parts of the Socket.IO codebase.

## Socket#handshake

This object contains some details about the handshake that happens at the beginning of the Socket.IO session.

```
{
  headers: /* the headers of the initial request */
  query: /* the query params of the initial request */
  auth: /* the authentication payload */
  time: /* the date of creation (as string) */
  issued: /* the date of creation (unix timestamp) */
  url: /* the request URL string */
  address: /* the ip of the client */
  xdomain: /* whether the connection is cross-domain */
  secure: /* whether the connection is secure */
}
```

Example:

```json
{
  "headers": {
    "user-agent": "xxxx",
    "accept": "*/*",
    "host": "example.com",
    "connection": "close"
  },
  "query": {
    "EIO": "4",
    "transport": "polling",
    "t": "NNjNltH"
  },
  "auth": {
    "token": "<PASSWORD>"
  },
  "time": "Sun Nov 22 2020 01:33:46 GMT+0100 (Central European Standard Time)",
  "issued": 1606005226969,
  "url": "/socket.io/?EIO=4&transport=polling&t=NNjNltH",
  "address": "::ffff:1.2.3.4",
  "xdomain": false,
  "secure": true
}
```

## Socket#rooms

This is a reference to the [rooms](/docs/v4/rooms/) the Socket is currently in.

```js
io.on("connection", (socket) => {
  console.log(socket.rooms); // Set { <socket.id> }
  socket.join("room1");
  console.log(socket.rooms); // Set { <socket.id>, "room1" }
});
```

## Socket#data

An arbitrary object that can be used in conjunction with the `fetchSockets()` utility method:

```js
// server A
io.on("connection", (socket) => {
  socket.data.username = "alice";
});

// server B
const sockets = await io.fetchSockets();
console.log(sockets[0].data.username); // "alice"
```

More information [here](/docs/v4/server-instance/#Utility-methods).

## Additional attributes

As long as you do not overwrite any existing attribute, you can attach any attribute to the Socket instance and use it later:

```js
// in a middleware
io.use(async (socket, next) => {
  try {
    const user = await fetchUser(socket);
    socket.user = user;
    next();
  } catch (e) {
    next(new Error("unknown user"));
  }
});

io.on("connection", (socket) => {
  console.log(socket.user);

  // in a listener
  socket.on("set username", (username) => {
    socket.username = username;
{'fraction_non_alphanumeric': 0.16175854849965107, 'fraction_numerical': 0.015212840195394277, 'mean_word_length': 3.7869071476285905, 'pattern_counts': {'":': 36, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6802532', 'n_tokens_mistral': 2486, 'n_tokens_neox': 2350, 'n_words': 707}