I haven't looked too deeply into it, but I'm fairly sure the basic issue you're running into is that you're not in correct rooms - NodeBB actually doesn't just emit the events to every socket connected, instead it specifically selects users who are considered online.
The first one requires the client to be in a uid_${uid}
room, while the latter is related to the way sockets report user activity and configurable via ACP (you can configure how long a client can be inactive before it's considered to be offline). I don't remember the specifics of the latter (I think you might just need to periodically send something), but the former requires your socket client to authenticate (you should be automatically put into an appropriate room if NodeBB can authenticate you).
It works with cookie auth just fine, but it's won't be used by default across domains since browsers have good reasons not to let you just use other website's cookie. You can make it work by adding the website your code will run under to Access-Control-Allow-Origin
(note: *
doesn't allow for credentials, you need to explicitly specify an origin here). which you can find under Settings>Advanced in NodeBB ACP (you may also need to set Access-Control-Allow-Credentials
to true
). Then you can just set withCredentials
option in socket.io client configuration to true
and it should work with your current logged-in user.
If your code is running in eg. NodeJS you'll need to set extraHeaders
option to an object like this:
{ cookie: `express.sid=${cookieValue}` }
As to how to get the cookie - authorize the user via any HTTP client and retrieve it from the response. Unfortunately AFAIK for socket.io
this is the only way to do this right now.
Which I guess is something that might be changed? Right now sockets just check the session cookie, but now that WriteAPI is integrated with NodeBB it should be fairly simple to allow using Bearer Tokens from the write API if no session cookie is provided. But that's probably a feature request for NodeBB GitHub Issues