A minimalistic social network, created using NodeJS + ReactJS.
The project consists of two parts:
frontfolder contains the Next.js appbackfolder contains the Express.js server
We use socket.io + express bundle. Socket_server.js is responsible for receiving messages and forwarding them.
const express = require ('express');
const app = express ();
const http = require ('http');
const server = http.createServer (app);
const {Server} = require ("socket.io");
const io = new Server (server);
const db_Worker = require ('./ DataBase_Worker');
const DataWorker = new db_Worker ("db.db")
const port = 3000
const hostname = "0.0.0.0"
app.use (express.static ('public'))
io.on ('connection', (socket) => {
DataWorker.excute_request (`INSERT INTO messages VALUES (" User connected ");`)
socket.on ('chat message', (msg) => {
// io.emit ('chat message', msg);
DataWorker.excute_request (`INSERT INTO messages VALUES (" $ {msg} ");`)
});
socket.on ('disconnect', () => {
DataWorker.excute_request (`INSERT INTO messages VALUES (" User disconnected ");`)
});
});
server.listen (port, hostname, () => console.log ("Server Up"))As you can see, it doesn't brodcast messages now and saves them to the database.
To enable the broadcast function, uncomment the line //io.emit('chat message ', msg);
| URL | DESCRIPTION | PARAMS | METHOD |
|---|---|---|---|
| /api/messages | Returns JSON with all messages | frequency | GET |
| /api/init_table | Initializes the messages table(dev) | None | GET |
| /api/new_message | Add message to database | msg, frequency | POST |
| /api/delete_message | Delete message by id | id | POST |
- something
Issues are welcome😄
If you want to add or remove something, then you can safely fork the project