From 86eb4227b2a7dc7c2e68b9c94db2403186ce1c16 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Sun, 31 Jan 2021 23:47:23 +0100 Subject: [PATCH] docs(examples): add example with traefik Reference: https://doc.traefik.io/traefik/v2.0/ --- examples/cluster-traefik/README.md | 22 ++ examples/cluster-traefik/docker-compose.yml | 27 ++ examples/cluster-traefik/server/Dockerfile | 15 + examples/cluster-traefik/server/index.js | 83 +++++ examples/cluster-traefik/server/package.json | 17 ++ .../cluster-traefik/server/public/index.html | 28 ++ .../cluster-traefik/server/public/main.js | 286 ++++++++++++++++++ .../cluster-traefik/server/public/style.css | 149 +++++++++ examples/cluster-traefik/traefik.yml | 10 + 9 files changed, 637 insertions(+) create mode 100644 examples/cluster-traefik/README.md create mode 100644 examples/cluster-traefik/docker-compose.yml create mode 100644 examples/cluster-traefik/server/Dockerfile create mode 100644 examples/cluster-traefik/server/index.js create mode 100644 examples/cluster-traefik/server/package.json create mode 100644 examples/cluster-traefik/server/public/index.html create mode 100644 examples/cluster-traefik/server/public/main.js create mode 100644 examples/cluster-traefik/server/public/style.css create mode 100644 examples/cluster-traefik/traefik.yml diff --git a/examples/cluster-traefik/README.md b/examples/cluster-traefik/README.md new file mode 100644 index 0000000000..66d4195465 --- /dev/null +++ b/examples/cluster-traefik/README.md @@ -0,0 +1,22 @@ + +# Socket.IO Chat with traefik & [redis](https://redis.io/) + +A simple chat demo for Socket.IO + +## How to use + +Install [Docker Compose](https://docs.docker.com/compose/install/), then: + +``` +$ docker-compose up -d +``` + +And then point your browser to `http://localhost:3000`. + +You can then scale the server to multiple instances: + +``` +$ docker-compose up -d --scale=server=7 +``` + +The session stickiness, which is [required](https://socket.io/docs/v3/using-multiple-nodes/) when using multiple Socket.IO server instances, is achieved with a cookie. More information [here](https://doc.traefik.io/traefik/v2.0/routing/services/#sticky-sessions). diff --git a/examples/cluster-traefik/docker-compose.yml b/examples/cluster-traefik/docker-compose.yml new file mode 100644 index 0000000000..cc558a0036 --- /dev/null +++ b/examples/cluster-traefik/docker-compose.yml @@ -0,0 +1,27 @@ +version: "3" + +services: + traefik: + image: traefik:2.4 + volumes: + - ./traefik.yml:/etc/traefik/traefik.yml + - /var/run/docker.sock:/var/run/docker.sock + links: + - server + ports: + - "3000:80" + - "8080:8080" + + server: + build: ./server + links: + - redis + labels: + - "traefik.http.routers.chat.rule=PathPrefix(`/`)" + - traefik.http.services.chat.loadBalancer.sticky.cookie.name=server_id + - traefik.http.services.chat.loadBalancer.sticky.cookie.httpOnly=true + + redis: + image: redis:6-alpine + labels: + - traefik.enable=false diff --git a/examples/cluster-traefik/server/Dockerfile b/examples/cluster-traefik/server/Dockerfile new file mode 100644 index 0000000000..caccfba307 --- /dev/null +++ b/examples/cluster-traefik/server/Dockerfile @@ -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" ] diff --git a/examples/cluster-traefik/server/index.js b/examples/cluster-traefik/server/index.js new file mode 100644 index 0000000000..134efc0577 --- /dev/null +++ b/examples/cluster-traefik/server/index.js @@ -0,0 +1,83 @@ +// Setup basic express server +var express = require('express'); +var app = express(); +var server = require('http').createServer(app); +var io = require('socket.io')(server); +var redis = require('socket.io-redis'); +var port = process.env.PORT || 3000; +var crypto = require('crypto'); +var serverName = crypto.randomBytes(3).toString('hex'); + +io.adapter(redis({ host: 'redis', port: 6379 })); + +server.listen(port, function () { + console.log('Server listening at port %d', port); + console.log('Hello, I\'m %s, how can I help?', serverName); +}); + +// Routing +app.use(express.static(__dirname + '/public')); + +// Chatroom + +var numUsers = 0; + +io.on('connection', function (socket) { + socket.emit('my-name-is', serverName); + + var addedUser = false; + + // when the client emits 'new message', this listens and executes + socket.on('new message', function (data) { + // we tell the client to execute 'new message' + socket.broadcast.emit('new message', { + username: socket.username, + message: data + }); + }); + + // when the client emits 'add user', this listens and executes + socket.on('add user', function (username) { + if (addedUser) return; + + // we store the username in the socket session for this client + socket.username = username; + ++numUsers; + addedUser = true; + socket.emit('login', { + numUsers: numUsers + }); + // echo globally (all clients) that a person has connected + socket.broadcast.emit('user joined', { + username: socket.username, + numUsers: numUsers + }); + }); + + // when the client emits 'typing', we broadcast it to others + socket.on('typing', function () { + socket.broadcast.emit('typing', { + username: socket.username + }); + }); + + // when the client emits 'stop typing', we broadcast it to others + socket.on('stop typing', function () { + socket.broadcast.emit('stop typing', { + username: socket.username + }); + }); + + // when the user disconnects.. perform this + socket.on('disconnect', function () { + if (addedUser) { + --numUsers; + + // echo globally that this client has left + socket.broadcast.emit('user left', { + username: socket.username, + numUsers: numUsers + }); + } + }); +}); diff --git a/examples/cluster-traefik/server/package.json b/examples/cluster-traefik/server/package.json new file mode 100644 index 0000000000..7b804427ee --- /dev/null +++ b/examples/cluster-traefik/server/package.json @@ -0,0 +1,17 @@ +{ + "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": { + "express": "4.13.4", + "socket.io": "^3.1.0", + "socket.io-redis": "^6.0.1" + }, + "scripts": { + "start": "node index.js" + } +} diff --git a/examples/cluster-traefik/server/public/index.html b/examples/cluster-traefik/server/public/index.html new file mode 100644 index 0000000000..9b2043d73f --- /dev/null +++ b/examples/cluster-traefik/server/public/index.html @@ -0,0 +1,28 @@ + + + + + Socket.IO Chat Example + + + + + + + + + + \ No newline at end of file diff --git a/examples/cluster-traefik/server/public/main.js b/examples/cluster-traefik/server/public/main.js new file mode 100644 index 0000000000..bceebafd4d --- /dev/null +++ b/examples/cluster-traefik/server/public/main.js @@ -0,0 +1,286 @@ +$(function() { + var FADE_TIME = 150; // ms + var TYPING_TIMER_LENGTH = 400; // ms + var COLORS = [ + '#e21400', '#91580f', '#f8a700', '#f78b00', + '#58dc00', '#287b00', '#a8f07a', '#4ae8c4', + '#3b88eb', '#3824aa', '#a700ff', '#d300e7' + ]; + + // Initialize variables + var $window = $(window); + var $usernameInput = $('.usernameInput'); // Input for username + var $messages = $('.messages'); // Messages area + var $inputMessage = $('.inputMessage'); // Input message input box + + var $loginPage = $('.login.page'); // The login page + var $chatPage = $('.chat.page'); // The chatroom page + + // Prompt for setting a username + var username; + var connected = false; + var typing = false; + var lastTypingTime; + var $currentInput = $usernameInput.focus(); + + var socket = io(); + + function addParticipantsMessage (data) { + var message = ''; + if (data.numUsers === 1) { + message += "there's 1 participant"; + } else { + message += "there are " + data.numUsers + " participants"; + } + log(message); + } + + // Sets the client's username + function setUsername () { + username = cleanInput($usernameInput.val().trim()); + + // If the username is valid + if (username) { + $loginPage.fadeOut(); + $chatPage.show(); + $loginPage.off('click'); + $currentInput = $inputMessage.focus(); + + // Tell the server your username + socket.emit('add user', username); + } + } + + // Sends a chat message + function sendMessage () { + var message = $inputMessage.val(); + // Prevent markup from being injected into the message + message = cleanInput(message); + // if there is a non-empty message and a socket connection + if (message && connected) { + $inputMessage.val(''); + addChatMessage({ + username: username, + message: message + }); + // tell server to execute 'new message' and send along one parameter + socket.emit('new message', message); + } + } + + // Log a message + function log (message, options) { + var $el = $('
  • ').addClass('log').text(message); + addMessageElement($el, options); + } + + // Adds the visual chat message to the message list + function addChatMessage (data, options) { + // Don't fade the message in if there is an 'X was typing' + var $typingMessages = getTypingMessages(data); + options = options || {}; + if ($typingMessages.length !== 0) { + options.fade = false; + $typingMessages.remove(); + } + + var $usernameDiv = $('') + .text(data.username) + .css('color', getUsernameColor(data.username)); + var $messageBodyDiv = $('') + .text(data.message); + + var typingClass = data.typing ? 'typing' : ''; + var $messageDiv = $('
  • ') + .data('username', data.username) + .addClass(typingClass) + .append($usernameDiv, $messageBodyDiv); + + addMessageElement($messageDiv, options); + } + + // Adds the visual chat typing message + function addChatTyping (data) { + data.typing = true; + data.message = 'is typing'; + addChatMessage(data); + } + + // Removes the visual chat typing message + function removeChatTyping (data) { + getTypingMessages(data).fadeOut(function () { + $(this).remove(); + }); + } + + // Adds a message element to the messages and scrolls to the bottom + // el - The element to add as a message + // options.fade - If the element should fade-in (default = true) + // options.prepend - If the element should prepend + // all other messages (default = false) + function addMessageElement (el, options) { + var $el = $(el); + + // Setup default options + if (!options) { + options = {}; + } + if (typeof options.fade === 'undefined') { + options.fade = true; + } + if (typeof options.prepend === 'undefined') { + options.prepend = false; + } + + // Apply options + if (options.fade) { + $el.hide().fadeIn(FADE_TIME); + } + if (options.prepend) { + $messages.prepend($el); + } else { + $messages.append($el); + } + $messages[0].scrollTop = $messages[0].scrollHeight; + } + + // Prevents input from having injected markup + function cleanInput (input) { + return $('
    ').text(input).text(); + } + + // Updates the typing event + function updateTyping () { + if (connected) { + if (!typing) { + typing = true; + socket.emit('typing'); + } + lastTypingTime = (new Date()).getTime(); + + setTimeout(function () { + var typingTimer = (new Date()).getTime(); + var timeDiff = typingTimer - lastTypingTime; + if (timeDiff >= TYPING_TIMER_LENGTH && typing) { + socket.emit('stop typing'); + typing = false; + } + }, TYPING_TIMER_LENGTH); + } + } + + // Gets the 'X is typing' messages of a user + function getTypingMessages (data) { + return $('.typing.message').filter(function (i) { + return $(this).data('username') === data.username; + }); + } + + // Gets the color of a username through our hash function + function getUsernameColor (username) { + // Compute hash code + var hash = 7; + for (var i = 0; i < username.length; i++) { + hash = username.charCodeAt(i) + (hash << 5) - hash; + } + // Calculate color + var index = Math.abs(hash % COLORS.length); + return COLORS[index]; + } + + // Keyboard events + + $window.keydown(function (event) { + // Auto-focus the current input when a key is typed + if (!(event.ctrlKey || event.metaKey || event.altKey)) { + $currentInput.focus(); + } + // When the client hits ENTER on their keyboard + if (event.which === 13) { + if (username) { + sendMessage(); + socket.emit('stop typing'); + typing = false; + } else { + setUsername(); + } + } + }); + + $inputMessage.on('input', function() { + updateTyping(); + }); + + // Click events + + // Focus input when clicking anywhere on login page + $loginPage.click(function () { + $currentInput.focus(); + }); + + // Focus input when clicking on the message input's border + $inputMessage.click(function () { + $inputMessage.focus(); + }); + + // Socket events + + // Whenever the server emits 'login', log the login message + socket.on('login', function (data) { + connected = true; + // Display the welcome message + var message = "Welcome to Socket.IO Chat – "; + log(message, { + prepend: true + }); + addParticipantsMessage(data); + }); + + // Whenever the server emits 'new message', update the chat body + socket.on('new message', function (data) { + addChatMessage(data); + }); + + // Whenever the server emits 'user joined', log it in the chat body + socket.on('user joined', function (data) { + log(data.username + ' joined'); + addParticipantsMessage(data); + }); + + // Whenever the server emits 'user left', log it in the chat body + socket.on('user left', function (data) { + log(data.username + ' left'); + addParticipantsMessage(data); + removeChatTyping(data); + }); + + // Whenever the server emits 'typing', show the typing message + socket.on('typing', function (data) { + addChatTyping(data); + }); + + // Whenever the server emits 'stop typing', kill the typing message + socket.on('stop typing', function (data) { + removeChatTyping(data); + }); + + socket.on('disconnect', function () { + log('you have been disconnected'); + }); + + socket.on('connect', function () { + if (username) { + log('you have been reconnected'); + socket.emit('add user', username); + } + }); + + socket.io.on('reconnect_error', function () { + log('attempt to reconnect has failed'); + }); + + socket.on('my-name-is', function (serverName) { + log('host is now ' + serverName); + }) + +}); diff --git a/examples/cluster-traefik/server/public/style.css b/examples/cluster-traefik/server/public/style.css new file mode 100644 index 0000000000..3052d88e4c --- /dev/null +++ b/examples/cluster-traefik/server/public/style.css @@ -0,0 +1,149 @@ +/* Fix user-agent */ + +* { + box-sizing: border-box; +} + +html { + font-weight: 300; + -webkit-font-smoothing: antialiased; +} + +html, input { + font-family: + "HelveticaNeue-Light", + "Helvetica Neue Light", + "Helvetica Neue", + Helvetica, + Arial, + "Lucida Grande", + sans-serif; +} + +html, body { + height: 100%; + margin: 0; + padding: 0; +} + +ul { + list-style: none; + word-wrap: break-word; +} + +/* Pages */ + +.pages { + height: 100%; + margin: 0; + padding: 0; + width: 100%; +} + +.page { + height: 100%; + position: absolute; + width: 100%; +} + +/* Login Page */ + +.login.page { + background-color: #000; +} + +.login.page .form { + height: 100px; + margin-top: -100px; + position: absolute; + + text-align: center; + top: 50%; + width: 100%; +} + +.login.page .form .usernameInput { + background-color: transparent; + border: none; + border-bottom: 2px solid #fff; + outline: none; + padding-bottom: 15px; + text-align: center; + width: 400px; +} + +.login.page .title { + font-size: 200%; +} + +.login.page .usernameInput { + font-size: 200%; + letter-spacing: 3px; +} + +.login.page .title, .login.page .usernameInput { + color: #fff; + font-weight: 100; +} + +/* Chat page */ + +.chat.page { + display: none; +} + +/* Font */ + +.messages { + font-size: 150%; +} + +.inputMessage { + font-size: 100%; +} + +.log { + color: gray; + font-size: 70%; + margin: 5px; + text-align: center; +} + +/* Messages */ + +.chatArea { + height: 100%; + padding-bottom: 60px; +} + +.messages { + height: 100%; + margin: 0; + overflow-y: scroll; + padding: 10px 20px 10px 20px; +} + +.message.typing .messageBody { + color: gray; +} + +.username { + font-weight: 700; + overflow: hidden; + padding-right: 15px; + text-align: right; +} + +/* Input */ + +.inputMessage { + border: 10px solid #000; + bottom: 0; + height: 60px; + left: 0; + outline: none; + padding-left: 10px; + position: absolute; + right: 0; + width: 100%; +} diff --git a/examples/cluster-traefik/traefik.yml b/examples/cluster-traefik/traefik.yml new file mode 100644 index 0000000000..73674199b3 --- /dev/null +++ b/examples/cluster-traefik/traefik.yml @@ -0,0 +1,10 @@ + +api: + insecure: true + +entryPoints: + web: + address: ":80" + +providers: + docker: {}