├── README.md
├── app.js
├── index.html
├── server.js
└── styles.css
/README.md:
--------------------------------------------------------------------------------
1 | # Chat-Application
2 | A real-time chat application.
3 |
4 | # Technologies
5 | Node.js, Express, Socket.io, HTML, CSS
6 |
--------------------------------------------------------------------------------
/app.js:
--------------------------------------------------------------------------------
1 | const socket = io();
2 |
3 | document.addEventListener('DOMContentLoaded', function() {
4 | const form = document.getElementById('message-form');
5 | const messageInput = document.getElementById('message-input');
6 | const messagesDiv = document.getElementById('messages');
7 |
8 | form.addEventListener('submit', function(e) {
9 | e.preventDefault();
10 | socket.emit('sendMessage', messageInput.value);
11 | messageInput.value = '';
12 | });
13 |
14 | socket.on('receiveMessage', function(message) {
15 | const messageElement = document.createElement('div');
16 | messageElement.textContent = message;
17 | messagesDiv.appendChild(messageElement);
18 | messagesDiv.scrollTop = messagesDiv.scrollHeight;
19 | });
20 | });
21 |
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Chat App
7 |
8 |
9 |
10 |
11 |
Chat Application
12 |
13 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/server.js:
--------------------------------------------------------------------------------
1 | const express = require('express');
2 | const http = require('http');
3 | const socketIo = require('socket.io');
4 |
5 | const app = express();
6 | const server = http.createServer(app);
7 | const io = socketIo(server);
8 |
9 | app.use(express.static('public'));
10 |
11 | io.on('connection', (socket) => {
12 | console.log('New client connected');
13 |
14 | socket.on('sendMessage', (message) => {
15 | io.emit('receiveMessage', message);
16 | });
17 |
18 | socket.on('disconnect', () => {
19 | console.log('Client disconnected');
20 | });
21 | });
22 |
23 | const PORT = process.env.PORT || 3000;
24 | server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
25 |
--------------------------------------------------------------------------------
/styles.css:
--------------------------------------------------------------------------------
1 | body {
2 | font-family: Arial, sans-serif;
3 | margin: 0;
4 | padding: 0;
5 | display: flex;
6 | justify-content: center;
7 | align-items: center;
8 | height: 100vh;
9 | background-color: #f4f4f4;
10 | }
11 |
12 | #chat-app {
13 | background-color: white;
14 | padding: 20px;
15 | border-radius: 5px;
16 | box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
17 | width: 300px;
18 | }
19 |
20 | h1 {
21 | margin: 0 0 20px;
22 | text-align: center;
23 | }
24 |
25 | #messages {
26 | height: 200px;
27 | overflow-y: auto;
28 | border: 1px solid #ddd;
29 | margin-bottom: 10px;
30 | padding: 10px;
31 | }
32 |
33 | #message-form {
34 | display: flex;
35 | justify-content: space-between;
36 | }
37 |
38 | #message-input {
39 | flex: 1;
40 | padding: 10px;
41 | font-size: 16px;
42 | border: 1px solid #ddd;
43 | border-radius: 3px;
44 | }
45 |
46 | button {
47 | padding: 10px 15px;
48 | font-size: 16px;
49 | border: none;
50 | background-color: #007bff;
51 | color: white;
52 | border-radius: 3px;
53 | cursor: pointer;
54 | }
55 |
56 | button:hover {
57 | background-color: #0056b3;
58 | }
59 |
--------------------------------------------------------------------------------