S
S
sema-fedotov2020-07-04 23:50:12
Node.js
sema-fedotov, 2020-07-04 23:50:12

Why is React not connecting via socket.io?

There is a react code like this:

import io from 'socket.io-client';
const socket = io('http://localhost:3000');
socket.on('connect', (d) => {
  socket.emit('new_connection', document.location.href.split('?')[1].replace(document.location.hash, ''))
})


node code:
var app = express() 
var http = require('http').createServer(app)
var io = require('socket.io')(http)

io.on('connection', (socket) => {
  socket.on('new_connection',  () => {
    console.log('ok')
  })
});



http.listen(3000, () => {
  console.log('listening on *:3000')
})


The bottom line is that everything works on the local (on my PC), it is worth uploading to the server, it does not connect there. I set up the path on Nginx, instead of 'http://localhost:3000'just trying to write, both the url and the ip of the machine do not want to connect. What could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nazariy Lazarchuk, 2020-07-04
@LazarchukNazar

1) Check the path on the ws connection, maybe you left localhost somewhere =)
2) Check if the server is turned on on the server (for example, create a test get-request and throw it with Ajax)
I think the problem is in the addresses. If there is an error on ws, then please throw it in the comments!)

M
Maxim Lambov, 2020-07-05
@maxilamb

Check CORS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question