docs(examples): update the nginx cluster example

Related: https://github.com/socketio/socket.io/discussions/3778

Reference: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#hash
This commit is contained in:
Damien Arrachequesne
2021-01-27 22:52:52 +01:00
parent 10aafbbc16
commit 0d10e6131b
7 changed files with 66 additions and 4 deletions

View File

@@ -22,6 +22,16 @@ Each node connects to the redis backend, which will enable to broadcast to every
$ docker-compose stop server-george
```
A `client` container is included in the `docker-compose.yml` file, in order to test the routing.
You can create additional `client` containers with:
```
$ docker-compose up -d --scale=client=10 client
# and then
$ docker-compose logs client
```
## Features
- Multiple users can join a chat room by each entering a unique username

View File

@@ -0,0 +1,15 @@
FROM node:14-alpine
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install --prod
# Bundle app source
COPY . /usr/src/app
EXPOSE 3000
CMD [ "npm", "start" ]

View File

@@ -0,0 +1,13 @@
const socket = require('socket.io-client')('ws://nginx');
socket.on('connect', () => {
console.log('connected');
});
socket.on('my-name-is', (serverName) => {
console.log(`connected to ${serverName}`);
});
socket.on('disconnect', (reason) => {
console.log(`disconnected due to ${reason}`);
});

View File

@@ -0,0 +1,15 @@
{
"name": "socket.io-chat",
"version": "0.0.0",
"description": "A simple chat client using socket.io",
"main": "index.js",
"author": "Grant Timmerman",
"private": true,
"license": "MIT",
"dependencies": {
"socket.io-client": "^2.4.0"
},
"scripts": {
"start": "node index.js"
}
}

View File

@@ -45,6 +45,11 @@ server-ringo:
environment:
- NAME=Ringo
client:
build: ./client
links:
- nginx
redis:
image: redis:alpine
expose:

View File

@@ -24,8 +24,12 @@ http {
}
upstream nodes {
# enable sticky session
ip_hash;
# enable sticky session with either "hash" (uses the complete IP address)
hash $remote_addr consistent;
# or "ip_hash" (uses the first three octets of the client IPv4 address, or the entire IPv6 address)
# ip_hash;
# or "sticky" (needs commercial subscription)
# sticky cookie srv_id expires=1h domain=.example.com path=/;
server server-john:3000;
server server-paul:3000;

View File

@@ -1,4 +1,4 @@
FROM mhart/alpine-node:6
FROM node:14-alpine
# Create app directory
RUN mkdir -p /usr/src/app
@@ -6,7 +6,7 @@ WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
RUN npm install --prod
# Bundle app source
COPY . /usr/src/app