Skip to content

SocketData type is not passed down to the Namespace #4229

Closed

Description

@OrkhanAlikhanov

SocketData type is not passed down to the Namespace when we do io.of().
That results in socket.data being any type:

Example:

const io = new Server<{},{},{},{ username: string }>()

io.on('connection', socket => {
   socket.data /// { username?: string }
})

io.of('/nps').on('connection', socket => {
   socket.data /// any
})

Source code:

socket.io/lib/index.ts

Lines 577 to 582 in 51784d0

public of(
name: string | RegExp | ParentNspNameMatchFn,
fn?: (
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
) => void
): Namespace<ListenEvents, EmitEvents, ServerSideEvents> {

Should be:

of(...): Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>;

Activity

  1. OrkhanAlikhanov commented on Jan 1, 2022

    @OrkhanAlikhanov
    ContributorAuthor

    Also it would be nice to have RemoteSocket.data type safe. Right now it defaults to any

  2. darrachequesne commented on Jan 1, 2022

    @darrachequesne
    Member

    @OrkhanAlikhanov that's a great idea! Would you have the time to open a pull request?

  3. OrkhanAlikhanov commented on Jan 3, 2022

    @OrkhanAlikhanov
    ContributorAuthor

    @darrachequesne When making RemoteSocket.data type safe, should BroadcastOperator<EmitEvents> also change to BroadcastOperator<EmitEvents, SocketData>? I ask because BroadcastOperator.fetchSockets is the method that returns RemoteSocket.

    There are 2 options:

    1. Make BroadcastOperator like BroadcastOperator<EmitEvents, SocketData = any>.
    2. Only update fetchSockets method signature:
    //from
    fetchSockets(): Promise<RemoteSocket<EmitEvents>[]>
    // to
    fetchSockets<SocketData = any>(): Promise<RemoteSocket<EmitEvents, SocketData>[]>

    I think it's better to go with second option. I don't see need to pass SocketData around BroadcastOperator

  4. added this to the 4.4.1 milestone on Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions