Can someone point me to how i would add a Socket IO Client to my javascript web application to be informed when topical activity occurs within select NodeBB categories
-
Maybe try with
{ "url": "http://127.0.0.1:4567", "secret": "9e7759e1-4cef-4eed-9a98-dbc7768e0b2b", "database": "mongo", "mongo": { "host": "127.0.0.1", "port": "27017", "username": "votf", "password": "votf", "database": "learning_center", "uri": "" }, "port": "4567", "socket.io": { "origins": "http://127.0.0.1:*", }, }
You also need to send a csrf_token in the query string like we do during connection https://github.com/NodeBB/NodeBB/blob/master/public/src/sockets.js#L20-L22
-
ah. maybe the CSRF token is the magic.. i actually did try the http://127.0.0.1:*.. How do I know, what value that CSRF token should have? Do I find that in the config.json? Do I set it to the "secret" in config.json?
In the file you linked above, you set it to the following:
"csrf_token": "0f80459650aa8085dcb0c87e714ae3517f17b9426c17354f03251fb44c0737d310b5b4779a380047c67e1107538d4ad85731284d14d64ed4429603e4317da456",
But that doesnt feel right. I had no clue how you created that token, so I didnt bother trying to set that.
-
@baris Got it.. ty .. thats starting to hang together for me!! will give that a go.
-
@baris
Well I'm crawling towards correct.. I am now using the token found in the call to /api/configI'm also using a config.json that has been set as follows:
{ "url": "http://localhost:4567", "secret": "9e7759e1-4cef-4eed-9a98-dbc7768e0b2b", "database": "mongo", "mongo": { "host": "127.0.0.1", "port": "27017", "username": "votf", "password": "votf", "database": "learning_center", "uri": "" }, "port": "4567", "socket.io": { "origins": "http://127.0.0.1:*" } }
I still see a 403
My current test client code:
import { io } from "socket.io-client"; const websocketAddress = "http://127.0.0.1:4567"; const ioParams = { reconnectionDelayMax: 10000, auth: { token: "123" }, query: { "_csrf": "3aa60581b3f0fdf2091d68a3851bb62a1a1f3e6bd4a9774c8bf10873baf1a984845725af217817d41d3985ebce3df211cd98d6173651840adcf41f453c19a5c0" } }; export const socket = io(websocketAddress, ioParams); socket.on("connect", () => { console.log("yay"); })
I've also updated the Access-Control-Allow-Origin in the ACP as follows (not really knowing what to do):
Previously, all I needed in the ACP was the first parameter: http://localhost:3000 (that allowed me to get the NodeBB API calls working)
-
I just reviewed how I set up my own "dev socket io server" in the past. I did so with the following cors setting (in this case this was strictly a dev server, so "*" was ok).
But I'm pretty sure the 403 is because we've not hit the right CORS magic that is compatible with how NodeBB is configuring the SocketIO server..
Its the NodeBB server that is returning 403.
This is what I see in terms of the socket io cors setup in the NodeBB codebase.
Perhaps this suggests that I must also add a "cors" option to the "socket.io" object in config.json
Something like the following?
{ "url": "http://localhost:4567", "secret": "9e7759e1-4cef-4eed-9a98-dbc7768e0b2b", "database": "mongo", "mongo": { "host": "127.0.0.1", "port": "27017", "username": "votf", "password": "votf", "database": "learning_center", "uri": "" }, "port": "4567", "socket.io": { "origins": "http://127.0.0.1:*", "cors": "http://127.0.0.1:*" } }
Though I just tried that, with a couple different settings for "cors" .. and none of that worked.
-
Not sure where to go next.. i've tried a number of variations.. seems like what I have should work, yet my socketio connection request is rejected because of CORS.
-
Maybe try changing "url" in your config.json to
http://127.0.0.1:4567
If you provide a cors in config.json it should be an object that gets passed to socket.io so something like.
"socket.io": { "cors": { origin: "http://127.0.0.1:*", methods: ['GET', 'POST'], allowedHeaders: ['content-type'], } }
-
@baris In your "allowedHeaders" field, do I use the words 'content-type' .. or does this need to be replaced with some value
-
That field is passed to https://github.com/expressjs/cors#configuration-options, you might be able to just leave it out
If not specified, defaults to reflecting the headers specified in the request's Access-Control-Request-Headers header.
Did you try changing your url field in config.json to
http://127.0.0.1:4567
? And change the connection code to also use127.0.0.1:4567
instead of localhost. -