Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SocketData type is not passed down to the Namespace #4229

Closed
OrkhanAlikhanov opened this issue Dec 29, 2021 · 3 comments · Fixed by #4233
Closed

SocketData type is not passed down to the Namespace #4229

OrkhanAlikhanov opened this issue Dec 29, 2021 · 3 comments · Fixed by #4233
Milestone

Comments

@OrkhanAlikhanov
Copy link
Contributor

OrkhanAlikhanov commented Dec 29, 2021

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>;
@OrkhanAlikhanov
Copy link
Contributor Author

OrkhanAlikhanov commented Jan 1, 2022

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

@darrachequesne
Copy link
Member

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

@OrkhanAlikhanov
Copy link
Contributor Author

OrkhanAlikhanov commented Jan 3, 2022

@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

darrachequesne pushed a commit that referenced this issue Jan 4, 2022
The `SocketData` type was only available on the main namespace.

Related: #4229
See also: fe8730c
darrachequesne pushed a commit that referenced this issue Jan 6, 2022
@darrachequesne darrachequesne added this to the 4.4.1 milestone Jan 6, 2022
dzad pushed a commit to dzad/socket.io that referenced this issue May 29, 2023
)

The `SocketData` type was only available on the main namespace.

Related: socketio#4229
See also: socketio@5d78eac
dzad pushed a commit to dzad/socket.io that referenced this issue May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants