'send socket broadcast to other server with nginx
i try to implement a user online list in my chat application. for the first i used one server instance, now im working with an nginx server and tree server instances. when i try to display my online users, i cant display via the sockets to other clients, that are on another server instance.
docker-compose.yml:
version : '3.2'
services:
redis:
image: redis:alpine
ports:
- "6379:6379"
networks:
- chatnetworks
nginx:
image: nginx:alpine
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
build:
context: .
dockerfile: ./DockerfileNginx
volumes_from:
- nostromo
- millennium_falcon
- spaceship_enterprise
ports:
- "4001:4001"
networks:
- chatnetworks
userdb:
image: mongo:latest
container_name: mongodb
volumes:
- userdb:/data/db
networks:
- chatnetworks
prometheus:
image: prom/prometheus:latest
container_name: prometheus
ports:
- 9090:9090
volumes:
- ./prometheus:/etc/prometheus
- prometheus-data:/prometheus
command: --web.enable-lifecycle --config.file=/etc/prometheus/prometheus.yml
networks:
- chatnetworks
grafana:
image: grafana/grafana:latest
container_name: grafana
restart: always
volumes:
- ./grafana/grafana.ini:/etc/grafana/grafana.ini
- ./grafana/provisioning:/etc/grafana/provisioning
- ./grafana/data:/var/lib/grafana
user: "1000"
ports:
- 3000:3000
networks:
- chatnetworks
nostromo:
build:
context: .
dockerfile: ./Dockerfile
ports:
- '4000'
restart: 'on-failure'
networks:
- chatnetworks
depends_on:
- redis
environment:
- APPID=Nostromo
millennium_falcon:
build:
context: .
dockerfile: ./Dockerfile
ports:
- '4000'
restart: 'on-failure'
networks:
- chatnetworks
depends_on:
- redis
environment:
- APPID=Millennium_Falcon
spaceship_enterprise:
build:
context: .
dockerfile: ./Dockerfile
ports:
- '4000'
restart: 'on-failure'
networks:
- chatnetworks
depends_on:
- redis
environment:
- APPID=RaumschiffEnterprise
volumes:
userdb:
prometheus-data:
networks:
chatnetworks:
server.js (part):
ocket.on('user-connected',(userName_)=>{
//console.log(userName_ + " joined " +socket.id)
//save the time when a user connected
startTime = new Date()
//push username and socket.id into array to display users in chat client
arrayUsers.push({
socket_id: socket.id,
userName_socket: userName_,
startTime_: startTime
})
//console.log(arrayUsers.length)
//console.log(arrayUsers.startTime_)
//increment metric
useravailable.inc(1)
console.log(arrayUsers)
//client message for users that a new user joined
socket.broadcast.emit('user-connected',userName_)
//update online-users list
socket.emit('online-users',arrayUsers)
socket.broadcast.emit('online-users',arrayUsers)
})
index.html (part, client):
socket.on('online-users',(arrayUsers)=>{
let arrayUserNamesOnline=[]
arrayUsers.forEach(elem=>{
arrayUserNamesOnline.push(elem.userName_socket)
})
alert("users online: " +arrayUserNamesOnline)
appendUser(arrayUserNamesOnline)
//arrayUserNamesOnline.splice(0,arrayUserNamesOnline.length)
})
function appendUser(arrayUserNamesOnline){
userArea.removeChild(userArea.lastElementChild)
let mainDiv = document.createElement('div')
var markup=`
<h4>${arrayUserNamesOnline}</h4>
`
mainDiv.innerHTML=markup
userArea.appendChild(mainDiv)
}
is there a way to make the arrayUsers array for every server instance available, i think the problem is that my clients are connecting to the different servers, so i need to interconnect them, but i dont know how i can make the array changeable for every server container.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
