How to have two backend nodejs servers communicate with each other to handle different tasks with k8s in mind

2/24/2021

I have an express server that handles all authentication(using token auth and have redis store sessions to control the amount of logins per device) and reading/writing to mongoDB.

I want to add chat for my users(using socket.io and socket.io-redis) and instead of coding it within the current backend I was wondering how I could keep it separate or if that is even a good idea.

I thought it would be because I'm deploying on a k8s cluster but I was wondering how to go about the communication between these two servers.

I'm using ingress nginx controller where I open up traffic to the backend in the load balancer. At first I thought I would be routing all traffic from the backend between the chat server and the users. But that's not the case as the chat server needs to connect to the users directly? So how would I secure the communication between the backend to the chat server as well as the communication between the chat server and the users?

Maybe I'm overthinking this but any insight is appreciated.

-- mitchldtn
kubernetes
node.js
socket.io
socket.io-redis

1 Answer

11/10/2021

What I ended up doing was creating an Auth server, a Main API server and a Chat server. The frontend pointing to auth.domain.com, api.domain.com, chat.domain.com.

The Auth server generates a JWT that is stored in redis when a user logs in. The Main API server and Chat server check redis to make sure the JWT is valid and then executes.

Because all this is running on a k8s cluster and can have multiple instances, I used Pub/Sub so even if a user isn't on the same websocket server(chat server), they will still receive.

-- mitchldtn
Source: StackOverflow