You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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";constsocket=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:
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||{};varargs=newArray(arguments.length-1),callbacks=this._callbacks['$'+event];for(vari=1;i<arguments.length;i++){args[i-1]=arguments[i];}if(callbacks){callbacks=callbacks.slice(0);for(vari=0,len=callbacks.length;i<len;i++){if(!!callbacks[i]&&typeofcallbacks[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.
The text was updated successfully, but these errors were encountered:
Emitter.prototype.on=Emitter.prototype.addEventListener=function(event,fn){if(typeoffn!=="function"){thrownewError("please provide a function");}this._callbacks=this._callbacks||{};(this._callbacks['$'+event]=this._callbacks['$'+event]||[]).push(fn);returnthis;};
What do you think?
That being said, we could also add a warning if there is no matching callback here:
Emitter.prototype.emit=function(event){this._callbacks=this._callbacks||{};varargs=newArray(arguments.length-1),callbacks=this._callbacks['$'+event];for(vari=1;i<arguments.length;i++){args[i-1]=arguments[i];}if(callbacks){callbacks=callbacks.slice(0);for(vari=0,len=callbacks.length;i<len;++i){callbacks[i].apply(this,args);}}else{console.warn(`no callback registered for ${event}`);}returnthis;};
(note: we'd have to remove these console.warn in production)
Thank you for your callback. It looks so much better this way!
In terms of production, there is no warning given in most of the codes. We can skip it for now to adapt to the project. But I think we should definitely show a warning based on an env value to be determined by the user. Because finding this solution required extra focus.
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.
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
Expected behavior
The code under the hood is as follows:
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.
Platform:
Additional context
I would be glad if you consider it.
The text was updated successfully, but these errors were encountered: