{"id":630,"date":"2024-05-23T07:29:23","date_gmt":"2024-05-23T07:29:23","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=630"},"modified":"2024-05-23T08:03:55","modified_gmt":"2024-05-23T08:03:55","slug":"nodejs-basic-chat-program","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2024\/05\/23\/nodejs-basic-chat-program\/","title":{"rendered":"[NODEJS]BASIC CHAT PROGRAM\uae30\ubcf8\ucc44\ud305\ud504\ub85c\uadf8\ub7a8"},"content":{"rendered":"\n<p>index.html : \ud074\ub77c\uc774\uc5b8\ud2b8 \ubd80\ubd84 Client part<br>index.js : \uc11c\ubc84\ubd80\ubd84 Server part<\/p>\n\n\n\n<p>&#8211; \uae30\ubcf8 \ucc44\ud305 \ud504\ub85c\uadf8\ub7a8\uc785\ub2c8\ub2e4.<br>&#8211; \uc774\uc804 \ud3ec\uc2a4\ud2b8 \ubd80\ubd84\uc744(\uc2e4\uc2dc\uac04 \ud1b5\uc2e0) \uc798 \uc774\ud574\ud55c\ub2e4\uba74 \uc774\ubc88 \ubd80\ubd84\ub3c4 \ud06c\uac8c \uc5b4\ub835\uc9c0 \uc54a\uc744 \uac81\ub2c8\ub2e4.<br>&#8211; \ub179\uc0c9 \uceec\ub7ec\ub85c \uac15\uc870\ub41c \ubd80\ubd84\uc740 \uc18c\ucf13 \ud1b5\uc2e0 \ubaa8\ub4c8\uacfc \uad00\ub828 \uc788\ub294 \ubd80\ubd84\uc785\ub2c8\ub2e4.<br>&#8211; \ud14d\uc2a4\ud2b8\ud544\ub4dc\uc5d0 \uc785\ub825\ub41c \uba54\uc138\uc9c0\ub97c \uc18c\ucf13 \ud1b5\uc2e0\uc73c\ub85c \uc11c\ubc84\uc5d0 \ubcf4\ub0b4\uba74 \uc11c\ubc84\uac00 \ub2e4\uc2dc \ubaa8\ub4e0 \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0\uac8c \uba54\uc138\uc9c0\ub97c \ubcf4\ub0c5\ub2c8\ub2e4. <br>&#8211; input.focus() : \ud14d\uc2a4\ud2b8\ud544\ub4dc\uc5d0 \ud56d\uc0c1 \ud3ec\ucee4\uc2a4\ub97c \uc720\uc9c0\ud569\ub2c8\ub2e4.<br>&#8211;  form.addEventListener(&#8216;submit&#8217;, (e) =&gt; { } : Send\ubc84\ud2bc\uc744 \ub20c\ub800\uc744\ub54c submit(\ub370\uc774\ud130\uc804\uc1a1)\uc744 \uc2e4\ud589\ud569\ub2c8\ub2e4.<\/p>\n\n\n\n<p>&#8211; This is a basic chat program<br>&#8211; If you understand the previous post part (real-time communication) well, this part won&#8217;t be too difficult.<br>&#8211; The parts highlighted in green are related to the socket communication module.<br>&#8211;  When you send the message entered in the text field to the server through socket communication, the server sends the message back to all clients.<br>&#8211; input.focus(): Always maintains focus on the text field.<br>&#8211; form.addEventListener(&#8216;submit&#8217;, (e) =&gt; { }: Executes submit (data transfer) when the Send button is pressed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ index.js\nconst express = require('express');\nconst { createServer } = require('node:http');\nconst { join } = require('node:path');\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-green-color\">const { Server } = require('socket.io');<\/mark>\n\nasync function main() {\n\n  const app = express();\n  const server = createServer(app);\n\n  const io = new Server(server, {\n    connectionStateRecovery: {},\n  });\n\n  app.get('\/', (req, res) =&gt; {\n      res.sendFile(join(__dirname, 'index.html'));\n  });\n\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-green-color\">  io.on('connection', async(socket) =&gt; {\n      console.log('a user connected');\n\n      socket.on('disconnect', () =&gt; {\n        console.log('user disconnected');\n      });\n    \n      socket.on('chat message', (msg) =&gt; {\n        console.log('message: ' + msg);\n      }); \n\n      socket.on('chat message', async (msg) =&gt; {\n        io.emit('chat message', msg);\n      });\n\n  });<\/mark>\n\n  const port = process.env.PORT || 3000;\n\n  server.listen(port, () =&gt; {\n    console.log(`server running at http:\/\/localhost:${port}`);\n  });\n  \n}\/\/end mail()\n\nmain();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ index.html\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\"&gt;\n    &lt;title&gt;chat&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;label&gt;chat basic&lt;\/label&gt;\n   <mark style=\"background-color:rgba(0, 0, 0, 0);color:#c3b514\" class=\"has-inline-color\"> &lt;ul id=\"messages\"&gt;&lt;\/ul&gt;<\/mark>\n    &lt;form id=\"form\" action=\"\"&gt;\n      &lt;input type=\"text\" id=\"input\" autocomplete=\"off\" \/&gt;&lt;button&gt;Send&lt;\/button&gt;\n    &lt;\/form&gt;\n   <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-green-color\"> &lt;script src=\"\/socket.io\/socket.io.js\"&gt;&lt;\/script&gt;<\/mark>\n    &lt;script&gt;\n    let counter = 0;\n    \n    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-green-color\">const socket = io();<\/mark>\n    const form = document.getElementById('form');\n    const input = document.getElementById('input');\n    const messages = document.getElementById('messages');\n    \n  <mark style=\"background-color:rgba(0, 0, 0, 0);color:#c3b514\" class=\"has-inline-color\">  input.focus();<\/mark>\n    <mark style=\"background-color:rgba(0, 0, 0, 0);color:#c3b514\" class=\"has-inline-color\">form.addEventListener('submit', (e) =&gt; {<\/mark>\n        console.log(input.value);\n        <mark style=\"background-color:rgba(0, 0, 0, 0);color:#0adcce\" class=\"has-inline-color\">e.preventDefault();<\/mark>\n        if (input.value) {     \n            <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-green-color\">socket.emit('chat message', input.value);<\/mark>\n            input.value = '';\n            input.focus();\n        }\n    <mark style=\"background-color:rgba(0, 0, 0, 0);color:#c3b514\" class=\"has-inline-color\">});<\/mark>\n\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-green-color\">    socket.on('chat message', (msg) =&gt; {\n      const item = document.createElement('li');\n      item.textContent = msg;\n      messages.appendChild(item);\n      window.scrollTo(0, document.body.scrollHeight);\n    });<\/mark>\n\n    &lt;\/script&gt;  \n    \n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>index.html : \ud074\ub77c\uc774\uc5b8\ud2b8 \ubd80\ubd84 Client partindex.js : \uc11c\ubc84\ubd80\ubd84 Server part &#8211; \uae30\ubcf8 \ucc44\ud305 \ud504\ub85c\uadf8\ub7a8\uc785\ub2c8\ub2e4.&#8211; \uc774\uc804 \ud3ec\uc2a4\ud2b8 \ubd80\ubd84\uc744(\uc2e4\uc2dc\uac04 \ud1b5\uc2e0) \uc798 \uc774\ud574\ud55c\ub2e4\uba74 \uc774\ubc88 \ubd80\ubd84\ub3c4 \ud06c\uac8c \uc5b4\ub835\uc9c0 \uc54a\uc744 \uac81\ub2c8\ub2e4.&#8211; \ub179\uc0c9 \uceec\ub7ec\ub85c \uac15\uc870\ub41c \ubd80\ubd84\uc740 \uc18c\ucf13 \ud1b5\uc2e0 \ubaa8\ub4c8\uacfc \uad00\ub828 \uc788\ub294 \ubd80\ubd84\uc785\ub2c8\ub2e4.&#8211; \ud14d\uc2a4\ud2b8\ud544\ub4dc\uc5d0 \uc785\ub825\ub41c \uba54\uc138\uc9c0\ub97c \uc18c\ucf13 \ud1b5\uc2e0\uc73c\ub85c \uc11c\ubc84\uc5d0 \ubcf4\ub0b4\uba74 \uc11c\ubc84\uac00 \ub2e4\uc2dc \ubaa8\ub4e0 \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0\uac8c \uba54\uc138\uc9c0\ub97c \ubcf4\ub0c5\ub2c8\ub2e4. &#8211; input.focus() : \ud14d\uc2a4\ud2b8\ud544\ub4dc\uc5d0 \ud56d\uc0c1 \ud3ec\ucee4\uc2a4\ub97c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-630","post","type-post","status-publish","format-standard","hentry","category-nodejs","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=630"}],"version-history":[{"count":9,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/630\/revisions"}],"predecessor-version":[{"id":640,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/630\/revisions\/640"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}