socketio / socketio/socket.io

about single parameter listener error, (.apply is not a function error)

Open
#5,111 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
Socketio on function requests event name as 1st parameter and callback as 2nd parameter. For this, a check is done under the hood, but it is not enough. We need to improve the control a little more. Because if the callback is not sent, we get the following error.

Screen Shot 2022-05-09 at 14 08 43

To Reproduce

Please fill the following code example:

Socket.IO server version: 4.4.1

Server

// it doesn't matter what

Socket.IO client version: 4.4.1

Client

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

const socket = io("ws://localhost:3000/", {});

socket.on("connect", () => {
  console.log(`connect ${socket.id}`);

  socket.on("mybestevent.v1") // causes an error.
});

socket.on("disconnect", () => {
  console.log("disconnect");
});

Expected behavior
The code under the hood is as follows:

Screen Shot 2022-05-09 at 14 08 55

I think we can update the code here as follows. We even issue a warning like in the else block, telling the user that he should send a callback. I think it would be a better use.

Emitter.prototype.emit = function(event) {
   this._callbacks = this._callbacks || {};
   
   var args = new Array(arguments.length - 1), callbacks = this._callbacks['$' + event];
  
   for (var i = 1; i < arguments.length; i++) {
      args[i - 1] = arguments[i];
   }

   if(callbacks) {
     callbacks = callbacks.slice(0);
     for (var i = 0, len = callbacks.length; i < len; i++) {
         if(!!callbacks[i] && typeof callbacks[i] === 'function') {
              callbacks[i].apply(this, args);
         }else {
              console.warn("Looks like you forgot to add a callback to a listener. Please check your listeners.")
         }
     }
   }
}

Platform:

  • Device: any
  • OS: any

Additional context
I would be glad if you consider it.

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

Start from the Emitter.prototype.emit listener-dispatch code shown in the issue and reproduce the client example where socket.on receives only an event name. Trace why the missing callback reaches .apply; done means the invalid listener no longer throws and produces the proposed warning or an equivalent clear diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
api, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.