5

I am using Socket.io v4.0.1 with typescript and a node/express server which works as intended. The problem is that on connection I want to emit to the client socket its sessionID and userID, which are addition attributes on my server socket instance, but typescript is throwing the following type errors.

io.on("connection", (socket: Socket): void => {
  console.log(socket.id);

  socket.emit("session", {
    sessionID: socket.sessionID, // property sessionID does not exist on type Socket<DefaultEventsMap,DefaultEventsMap>
    userID: socket.userID,// property userID does not exist on type Socket<DefaultEventsMap,DefaultEventsMap>
  });
});

Is there any way to add this additional attributes to the type definition without changing the type definitions itself (@types/socket.io) ?

socketio's additional attributes documentation

5
  • 1
    Hi @J.B.C, socket: Socket part of your code using socket.io's type definition but you can create your own and extend socket.io's Socket definition and add userID and sessionID attributes on it or as an alternative you can create a map object based on users socket.io as key, an object that contains sessionID and userID as values and use this value in global scope of your node app :) Commented May 3, 2021 at 22:17
  • hey @halilcakar thats correct, the problem with this (which I forgot to mention) is that the io.on("connection", (expects Socket type here) => {...code here}) function expects a Socket type variable and not a custom type like you are suggesting. although the second option seems possible (and probably what I'm going to use), it seems to be unconvenient. Commented May 4, 2021 at 0:58
  • 2
    I know it's expecting a Socket type, so that's why you can rename it while importing as BaseSocket or smt and create an Socket interface that extends BaseSocket and also have more properties on it. I think with Typescript this is an option Commented May 4, 2021 at 1:00
  • 1
    stackoverflow.com/questions/60933653/… Maybe do something like this =) The answer should also work for you too :) and if you have an open project on github please share a link, I would love to see how you use nodejs with typescript :) Commented May 4, 2021 at 1:00
  • @halilcakar github.com/juanCortelezzi/Websocket-chat-server this is the work in progress for my server, but it shows how i use nodejs with typescript. Thanks for the help. Commented May 5, 2021 at 2:33

3 Answers 3

8

The solution was to create my own interface, which extends form the Socket type and add the desired additional attributes.

interface ISocket extends Socket {
  name?: string;
  // other additional attributes here, example:
  // surname?: string;
}

Although I had already tried that, it threw me another error, which basically said that the types Socket and ISocket could not be compatible, because the property "name" was missing on type Socket and required in type ISocket.

The solution to that problem was to add the "?" in name?: string. This makes the field not required in the interface.

Thanks to @halilcakar for helping me out.

Sign up to request clarification or add additional context in comments.

Comments

2

Try to extend Socket interface:

declare module 'socket.io' {
  interface Socket {
    sessionID?: string;
    userID?: string;
    // other additional attributes here, example:
    // surname?: string;
  }
}

...

io.on("connection", (socket: Socket): void => {
  console.log(socket.id);

  socket.emit("session", {
    sessionID: socket.sessionID,  // OK
    userID: socket.userID,        // OK
  });
});

Comments

0

// ExtendedSocket.ts
import { Socket } from "socket.io";

interface ExtendedSocket extends Socket {
    sessionID?: string;
    userID?: string;
}

export default ExtendedSocket;

// app.ts
import ExtendedSocket from "./models/ExtendedSocket";
...
io.on("connection", (socket: ExtendedSocket): void => {

  socket.emit("session", {
   sessionID: socket.sessionID,
   userID: socket.userID,
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.