socketio / socketio/socket.io

Server Middleware returning next(new Error(.......)) is not sent to the client

Open
#5,112 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

package:socket.io-client question
Dominant language
TypeScript
Stars
63.2k
Forks
10.3k
Avg merge
11d 20h
Merged PRs (30d)
2

Description

Describe the bug

I have a middleware that returns an Error object in some particular situation. I see the error in the server's console but I do not see the error being sent to the client.

Server:"socket.io": "^4.5.0"
Client: "socket.io-client": "^4.5.0"

To Reproduce

Socket.IO server version: x4.5.0

Server

import { Event, Socket } from "socket.io";
import { ServiceLayer } from "../services/serviceLayer";
export const userActivateMiddleware =
  (serviceLayer: ServiceLayer, socket: Socket) =>
  async (event: Event, next: (err?: Error) => void) => {
    const user = socket.data.user;

    if (user === undefined) {
      return next(new Error("User Email Not validated"));
    } 
    return next();
  };

// Elsewhere:
socket.use(userActivateMiddleware(serviceLayer, socket));

Socket.IO client version: 4.5.0

Client

import { io } from "socket.io-client";

// Listeners
socket.on("connect_error", socketConnectError);
socket.on("error", socketError);

function socketError(err: any): void {
    console.log("Socket Error", err);
}
function socketConnectError(error: Error): void {
    console.log("socketConnectError", error);
}

// How the request starts
socket.emit(MsgUserPixelKind, message, (response: MsgUserPixelValidation) => {
    manageResponseFromMsgUserPixel(response);
});

Expected behavior
The emit is performed and the NodeJS Socket.io servers receive the request. I can set a breakpoint and see the request getting inside the middleware. In the case I am testing, a rejection, I see the code getting into the if and executing the return next(new Error("....."));. I am expected to get the client to be notified somehow (by the connect_error or error or by the callback. However, nothing.

Platform:

  • Device: Macbook Pro
  • OS: MacOS 12.1

Additional context
Looking at the Chrome's network ws tab, I see the request sent which collaborate that I can have the NodeJS server hit my breakpoint. However, no response. The callback is not called either.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository files or tests are named. Start with the server socket.use(userActivateMiddleware(...)) middleware and compare its next(new Error(...)) path with the client's connect_error, error, and emit callback listeners. Reproduce the Socket.IO 4.5.0 case and establish which client-visible error path should complete the request.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, typescript
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.