How to use websocket notifications from external app?
-
I want to integrate nodeBB to my web app as community forum. Do NodeBB websockets infraestucture allows my external app to listen for events like new topic, category, etc through socket.io channels? If it is possible, there is any example?
-
You should be able to connect a socket to NodeBB and listen for the socket events, yes. You'd have to make sure you app URL is listed in the origins whitelist.
There's no example. Can you share more about what you're trying to accomplish?
-
@pitaj What I want is to listen for notifications, for example: new topic, new post, etc. I realize how to connect, but how can I listen for specifics events? For example:
const socket = socketIOClient("ws://forum.example.com/socket.io");
socket.on("event:new_topic", data => {
console.log("Response: ", data.someValue)
}); -
@reinier-pupo okay so you want to listen for notifications.
You essentially want something like this:
https://github.com/NodeBB/NodeBB/blob/b46d2f93e64e6546fb295d396b830dcab9f0cbbd/public/src/client/header/notifications.js#L25 -
@pitaj Making a deep testing to connect my app to nodebb websocket, I realize that I have an error. I run this command in wscat:
wscat -c ws://forum.example.com/socket.io
and this is the response:
error: Unexpected server response: 502
The nginx config is:server { listen 80; server_name forum.example.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://127.0.0.1:4567; proxy_redirect off; # Socket.IO Support proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
The nodeBB config.json:
{ "url": "http://forum.example.com", "secret": "&//&%$$", "database": "mongo", "mongo": { "host": "127.0.0.1", "port": "27017", "username": "nodebb", "password": "MyPass", "database": "nodebb", "uri": "" }, "port": "4567", "socket.io": { "origins": ["*:*"], "transports": ["polling","websocket"] } }
Is correct the ws://forum.example.com/socket.io
address?
Is this the correct workaround to connect through ws to nodeBB? -
@pitaj said in How to use websocket notifications from external app?:
@reinier-pupo okay so you want to listen for notifications.
You essentially want something like this:
https://github.com/NodeBB/NodeBB/blob/b46d2f93e64e6546fb295d396b830dcab9f0cbbd/employee monitoring software/public/src/client/header/notifications.js#L25Yeah, thank you!! Exactly what i was looking for!