Skip to content

Commit

Permalink
docs(examples): update the nginx cluster example
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jan 28, 2021
1 parent 10aafbb commit 0d10e61
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 4 deletions.
10 changes: 10 additions & 0 deletions examples/cluster-nginx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions examples/cluster-nginx/client/Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
13 changes: 13 additions & 0 deletions examples/cluster-nginx/client/index.js
Original file line number Diff line number Diff line change
@@ -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}`);
});
15 changes: 15 additions & 0 deletions examples/cluster-nginx/client/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
5 changes: 5 additions & 0 deletions examples/cluster-nginx/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ server-ringo:
environment:
- NAME=Ringo

client:
build: ./client
links:
- nginx

redis:
image: redis:alpine
expose:
Expand Down
8 changes: 6 additions & 2 deletions examples/cluster-nginx/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions examples/cluster-nginx/server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM mhart/alpine-node:6
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
RUN npm install --prod

# Bundle app source
COPY . /usr/src/app
Expand Down

0 comments on commit 0d10e61

Please sign in to comment.