balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

warn: Attempted to call `sailsSockets.join`, but the first argument was not a socket. #4413

Open Melchyore opened 6 years ago

Melchyore commented 6 years ago

Sails version: 1.0.1 Node version: 8.8.1 NPM version: 5.4.2 DB adapter name: sails-postgre DB adapter version: 1.0.0 Operating system: Windows 7 Ultimate x64


Hello,

I have a basic HTML page containing a basic script:

io.socket.on('notifications', notifications => {
  console.log(notifications)
})

and then, in the server, after authentication step, I try to join a WS room, but it fails:

sails.sockets.join(req.socket, _user.username)
sails.sockets.broadcast(_user.username, 'notifications', { test: 'foo' })

and logs : warn: Attempted to callsailsSockets.join, but the first argument was not a socket.

console.log(req.socket)

... logs a Socket Object so, why does req.socket is not a socket?

I don't want to use post and get methods of socket.io, all what I want is emitting events from server to client.

sailsbot commented 6 years ago

@Melchyore Thanks for posting, we'll take a look as soon as possible.


For help with questions about Sails, click here. If you’re interested in hiring @sailsbot and her minions in Austin, click here.

MattGarnettWelsh commented 6 years ago

Was there ever a response or method for not using client side sockets to send HTTP requests but still allowing pub/sub broadcasts etc through client/server?

mikermcneil commented 6 years ago

@Melchyore @MattGarnettWelsh The first argument to sails.sockets.join() should be either a socket id or req (never req.socket):

https://sailsjs.com/documentation/reference/web-sockets/sails-sockets/join image

image

But always use req unless you reaaaaaallllly know what you're doing. (socket ids are node-process-specific and best avoided-- use rooms instead)

mikermcneil commented 6 years ago

@Melchyore re:

... logs a Socket Object so, why does req.socket is not a socket?

This is a confusing thing, unfortunately -- req.socket is a TCP socket instance for HTTP requests and a Socket.io socket for virtual socket requests. (That's actually why we changed this back in 2016-- we used to support both, when possible, but that was even more confusing, because developers kept passing in TCP sockets on accident)

MattGarnettWelsh commented 6 years ago

@mikermcneil Hi Mike, thanks for the response.

I guess my queries stem from the fact I'm not using sails sockets (specifically angular-sails) anymore (https://www.npmjs.com/package/angular-sails) as I noticed sails doesn't support WSS. Or am I wrong here?

In light of not supporting WSS I've moved to straight angular-http to handle client side http requests for my services. I'd still like sails-sockets to handle my live events though.

mikermcneil commented 5 years ago

@MattGarnettWelsh Seems like we're not quite on the same page: Sails supports secure WebSockets -- see https://ration.io and the Platzi course for an example deployment to Heroku that uses wss://

MattGarnettWelsh commented 5 years ago

@mikermcneil Hi Mike, funnily enough I did actually do your course a while back! We now use the sails implementation of secure websockets - thanks a lot.