Initial commit
This commit is contained in:
55
backend/server.js
Normal file
55
backend/server.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import http from 'http';
|
||||
import { Server } from 'socket.io';
|
||||
import amqp from 'amqplib/callback_api.js';
|
||||
import app from './app.js';
|
||||
import path from 'path';
|
||||
import express from 'express';
|
||||
|
||||
const server = http.createServer(app);
|
||||
const io = new Server(server);
|
||||
|
||||
const RABBITMQ_URL = 'amqp://localhost';
|
||||
const QUEUE = 'chat_messages';
|
||||
|
||||
const __dirname = path.resolve();
|
||||
const frontendPath = path.join(__dirname, 'path/to/your/frontend/build/folder');
|
||||
app.use(express.static(frontendPath));
|
||||
|
||||
app.get('*', (req, res) => {
|
||||
res.sendFile(path.join(frontendPath, 'index.html'));
|
||||
});
|
||||
|
||||
amqp.connect(RABBITMQ_URL, (err, connection) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
connection.createChannel((err, channel) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
channel.assertQueue(QUEUE, { durable: false });
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
console.log('A user connected');
|
||||
|
||||
channel.consume(QUEUE, (msg) => {
|
||||
const message = JSON.parse(msg.content.toString());
|
||||
io.emit('newMessage', message);
|
||||
}, { noAck: true });
|
||||
|
||||
socket.on('newMessage', (message) => {
|
||||
channel.sendToQueue(QUEUE, Buffer.from(JSON.stringify(message)));
|
||||
});
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
console.log('A user disconnected');
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(3001, () => {
|
||||
console.log('Server is running on port 3001');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user