generated from lucien/api-template
add: improved the frontend and the backend by adding websockets
This commit is contained in:
parent
ecf7b61aca
commit
fbf7821320
18 changed files with 255 additions and 65 deletions
|
@ -11,6 +11,16 @@ router.get('/', async (req, res) => {
|
|||
res.send(channels);
|
||||
});
|
||||
|
||||
const sockets = new Set();
|
||||
|
||||
router.ws('/', function(ws, req) {
|
||||
sockets.add(ws);
|
||||
|
||||
ws.on('close', () => {
|
||||
sockets.delete(ws);
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/:name', async (req, res) => {
|
||||
const name = req.params.name;
|
||||
const connection = await getConnection();
|
||||
|
@ -23,6 +33,22 @@ router.get('/:name', async (req, res) => {
|
|||
}
|
||||
});
|
||||
|
||||
const channelSockets = new Map();
|
||||
|
||||
router.ws('/:name', function(ws, req) {
|
||||
const name = req.params.name;
|
||||
|
||||
if (!channelSockets.has(name)) {
|
||||
channelSockets.set(name, new Set());
|
||||
}
|
||||
|
||||
channelSockets.get(name).add(ws);
|
||||
|
||||
ws.on('close', () => {
|
||||
channelSockets.get(name).delete(ws);
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/:name/messages', async (req, res) => {
|
||||
const name = req.params.name;
|
||||
const connection = await getConnection();
|
||||
|
@ -78,6 +104,23 @@ router.post('/:name/messages/send', async (req, res) => {
|
|||
}
|
||||
|
||||
connection.end();
|
||||
|
||||
if (sockets.size > 0) {
|
||||
for (const client of sockets) {
|
||||
if (client.readyState === 1) {
|
||||
client.send('new_message');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (channelSockets.has(name)) {
|
||||
for (const client of channelSockets.get(name)) {
|
||||
if (client.readyState === 1) {
|
||||
client.send('new_message');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.send({ message: 'Message sent' });
|
||||
});
|
||||
|
||||
|
@ -113,6 +156,23 @@ router.post('/:name/messages/delete', async (req, res) => {
|
|||
await deleteMessage(connection, message_id);
|
||||
await deleMentions(connection, message_id);
|
||||
connection.end();
|
||||
|
||||
if (sockets.size > 0) {
|
||||
for (const client of sockets) {
|
||||
if (client.readyState === 1) {
|
||||
client.send('delete_message');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (channelSockets.has(name)) {
|
||||
for (const client of channelSockets.get(name)) {
|
||||
if (client.readyState === 1) {
|
||||
client.send('delete_message');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.send({ message: 'Message deleted' });
|
||||
});
|
||||
|
||||
|
@ -140,6 +200,15 @@ router.post('/add', async (req, res) => {
|
|||
|
||||
await addChannel(connection, name, description, user.id);
|
||||
connection.end();
|
||||
|
||||
if (sockets.size > 0) {
|
||||
for (const client of sockets) {
|
||||
if (client.readyState === 1) {
|
||||
client.send('new_channel');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.send({ message: 'Channel added' });
|
||||
});
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
const express = require('express');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const { getConnection, getLastMessages, getMentions } = require('../libs/mysql');
|
||||
|
||||
const router = express.Router();
|
||||
|
|
13
back/api/newchannels.js
Normal file
13
back/api/newchannels.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
const express = require('express');
|
||||
const { getConnection, getNewChannels } = require('../libs/mysql');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
const connection = await getConnection();
|
||||
const channels = await getNewChannels(connection);
|
||||
connection.end();
|
||||
res.send(channels);
|
||||
});
|
||||
|
||||
module.exports = router;
|
|
@ -7,6 +7,7 @@ const cors = require("cors");
|
|||
require("dotenv").config();
|
||||
|
||||
const app = express();
|
||||
var expressWs = require('express-ws')(app);
|
||||
const port = config.port || 3000;
|
||||
|
||||
app.use(express.json());
|
||||
|
|
|
@ -110,7 +110,7 @@ function getActiveChannels(connection) {
|
|||
FROM messages
|
||||
JOIN channels ON messages.channel_id = channels.id
|
||||
JOIN users ON messages.user_id = users.id
|
||||
WHERE date > (SELECT max(date) FROM messages) - 7 * 24 * 60 * 60
|
||||
WHERE date > (SELECT max(date) FROM messages) - 3 * 24 * 60 * 60
|
||||
GROUP BY channel_id
|
||||
ORDER BY count(*) DESC
|
||||
LIMIT 5;`,
|
||||
|
@ -124,6 +124,23 @@ function getActiveChannels(connection) {
|
|||
});
|
||||
}
|
||||
|
||||
function getNewChannels(connection) {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(
|
||||
`SELECT channels.id, name, description, owner_id, username AS owner_username
|
||||
FROM channels
|
||||
JOIN users ON channels.owner_id = users.id
|
||||
ORDER BY channels.id DESC LIMIT 5`,
|
||||
(error, result) => {
|
||||
if (error) {
|
||||
reject(new Error(error));
|
||||
}
|
||||
resolve(result);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function searchChannels(connection, search) {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(
|
||||
|
@ -319,6 +336,7 @@ module.exports = {
|
|||
getUserLastMessages,
|
||||
getChannels,
|
||||
getActiveChannels,
|
||||
getNewChannels,
|
||||
searchChannels,
|
||||
getChannel,
|
||||
addChannel,
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.3.1",
|
||||
"express": "^4.18.2",
|
||||
"express-ws": "^5.0.2",
|
||||
"fs": "^0.0.1-security",
|
||||
"https": "^1.0.0",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue