Answer the question
In order to leave comments, you need to log in
How to combine rest api + socket.io?
Good evening.
Essence of the question: Frontend on react, backend works on express. There was a task to add realtime chat.
As I understand it, the socket should be made on a port different from the one on which the backend is running, or is it not necessary ?. Will they have a conflict? How to implement it better
Direct your thoughts in the right direction)
Answer the question
In order to leave comments, you need to log in
A fairly simple and understandable solution - Socket.IO
There are many implementation examples, here is one of them
Back and sockets on the same port (there will be no conflict)
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);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log('a user connected');
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question