A memo that containerizes the simple chat application of Node.js + socket.io

background

A memo when containerizing the simple chat application of Node.js + socket.io

Link Collection

Make Node.js web application Docker

A document that creates a simple Node.js server that returns Hello World and containerizes it. I was able to regain the memory of Node.js, which I almost forgot.

Socket.io#Get Started You can make a simple app using socket.io

Sample code

package.json


{
  "name": "docker_web_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "shogo suzuki",
  "main": "socket_server.ts",
  "scripts": {
    "start": "node socket_server.js"
  },
  "dependencies": {
    "express": "*",
    "socket.io": "^2.3.0"
  }
}

Dockerfile



FROM node:14.13
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "socket_server.ts" ]

index.html



<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: 0.5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <script src="/socket.io/socket.io.js"></script>
  <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
  <script>
    $(function () {
      var socket = io();
      $('form').submit(function(e){
        e.preventDefault(); // prevents page reloading
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
      });
    });
  </script>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
  </body>
</html>

socket_server.ts



var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {
  console.log('a user connected');
  socket.on('chat message', (msg) => {
    io.emit('chat message', msg);
  });
});

http.listen(3000, () => {
  console.log('listening on *:3000');
});

Run

docker build -t socket-example:1.0 .
docker run -p 3000:3000 -d socket-example:1.0

Recommended Posts

A memo that containerizes the simple chat application of Node.js + socket.io
Check the operation of two roles with a chat application
A memo that was soberly addicted to the request of multipart / form-data
A simple example of a Servlet that displays Japanese
[Solution] A memo that I had a hard time because the format of sinatra-validation changed
A memo of the program that allows you to realize that the probability of dice rolling is about 1/6
[Personal application work memo] Move the location of the follow button
Determine that the value is a multiple of 〇 in Ruby
A program that counts the number of words in a List
A story that made me regret when a "NotReadablePropertyException" occurred during the development of the Spring Boot application.
A story that struggled with the introduction of Web Apple Pay
Let's create a TODO application in Java 5 Switch the display of TODO
Let's make a custom_cop that points out the shaking of the name
A story that confirmed the profile of Yasuko Sawaguchi 36 years ago
This and that of the JDK
A memorandum of the FizzBuzz problem
A memo that touched Spring Boot
Incident (rails) that is logged out when user editing of the application
A memo when you want to clear the time part of the calendar
A collection of phrases that impresses the "different feeling" of Java and JavaScript
Android application: Let's explain the mechanism of screen transition with simple code
Sample program that returns the hash value of a file in Java
Deploy a Node.js application to an ECS instance using the Cloud Toolkit
A simple application (CT) that allows you to view the RDSR in which the radiation exposure dose is recorded.