Skip to content

Instantly share code, notes, and snippets.

Created February 18, 2019 10:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
<!DOCTYPE html>
<title>Simple Group Chat on Node.js</title>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #fff; padding: 3px; position: fixed; bottom: 0; width: 100%; border-color: #000; border-top-style: solid; border-top-width: 1px;}
form input { border-style: solid; border-width: 1px; padding: 10px; width: 85%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; margin-left: 2%; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
<script src="../../"></script>
<script src=""></script>
<ul id="messages"></ul>
<form action="/" method="POST" id="chatForm">
<input id="txt" autocomplete="off" autofocus="on" oninput="isTyping()" placeholder="type your message here..." /><button>Send</button>
var socket = io.connect('http://localhost:8080');
// submit text message without reload/refresh the page
e.preventDefault(); // prevents page reloading
socket.emit('chat_message', $('#txt').val());
return false;
// append the chat text message
socket.on('chat_message', function(msg){
// append text if someone is online
socket.on('is_online', function(username) {
// ask username
var username = prompt('Please tell me your name');
socket.emit('username', username);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment