Realtime (Socket.IO) from a Next.js frontend — users created via Write API, uid stored in our DB

17 days ago by arnav123 to c/general-discussion

Hi — I need help getting realtime working between our Next.js app and a self-hosted NodeBB instance.

We are not embedding the NodeBB UI. NodeBB is the community backend only. Our product UI (posts, forums, chats) lives in Next.js and talks to NodeBB over the Write API / Read API.

What already works

On first login we:

  1. Create a NodeBB user with the Write API as admin, e.g. POST /api/v3/users with Authorization: Bearer and _uid = admin uid
  2. Store the returned uid on our user row (Supabase: users.nodebb_id)
  3. For later API calls we impersonate that user with the master token + _uid = stored nodebb_id

REST is fine: topics, posts, votes, bookmarks, categories, chats list/send all work.

What does not work

Realtime does not connect from the Next.js app (browser) to NodeBB Socket.IO.

We need live:

  • new posts / replies in the feed
  • likes / bookmarks
  • chat messages (chats.receive, unread counts)

Because the browser is a different origin than NodeBB, a direct io(nodebbOrigin) from the Next.js site is what we are trying to get right (CORS, cookies, auth).

How we tried to authenticate the socket

We create a user token server-side:

POST /api/v3/users/:uid/tokens (admin master token + _uid = admin)

Then we planned to connect from the browser with something like:

io(NODEBB_ORIGIN, {
  transports: ["websocket", "polling"],
  auth: { token }, // or query: { token }
});

Can someone please suggest that how we can do this thing.
baris 1 point 17 days ago

You likely need to add a socket.io block to your nodebb config.json and add the next.js app domain in there.

Something like:

"socket.io" :{
  "transports": ["polling", "websocket"],
  "origins": "https://example.org:*,https://example.net/:*"
}
path: 0 26029929, hotness: undefined, score: 1, children: 1
arnav123 1 point 16 days ago

> @baris said: > > You likely need to add a socket.io block to your nodebb config.json and add the next.js app domain in there. > > Something like: > > > "socket.io" :{ > "transports": ["polling", "websocket"], > "origins": "https://example.org:*,https://example.net/:*" > } >

Tried the socket.io origins block in config.json (with our Next app origin) and restarted NodeBB. Session Sharing still maps the user (POST /api/session-sharing/user → uid 3) but HTML pages (/, /recent, etc.) 302 and never set express.sid, so /api/config stays guest. We’re connecting from the Next server, not the browser. How do we get a session cookie after process() succeeds?

path: 0 26029929 26052820, hotness: undefined, score: 1, children: 0
general-discussion
general-discussion

@community.nodebb.org

login for more options
0
121
16

A place to talk about whatever you want

go to feed...