V
V
vadimparinov2021-05-02 21:53:46
Node.js
vadimparinov, 2021-05-02 21:53:46

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

2 answer(s)
Y
Yuriy Vorobyov, 2021-05-02
@YuriyVorobyov1333

A fairly simple and understandable solution - Socket.IO
There are many implementation examples, here is one of them

V
vItalIyIrtlach, 2021-05-02
@w13vitaliy

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 question

Ask a Question

731 491 924 answers to any question