Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

299
Vistas
Socket.io gives same ip address no matter where the connection is happening from (hosting with repl.it)

This is for a bigger project but i managed to condense it into one small program.

I am trying to get the ip address of the client from a websocket connection. I found out that I can use socket.handshake.address but when I run it on repl.it it always says the ip address is 172.18.0.1 no madder where I am connected from.

Index.js

const express = require('express');


const http = require('http').createServer();
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);


app.get('/', (request, response) => {
    response.sendFile('/home/runner/basicSocketio/index.html');

    });


io.on('connection', (socket) => {
  io.emit("message", "hello client")

    socket.on('message', (message) => {
    console.log(socket.handshake.address)//always prints ::ffff:172.18.0.1
  })
})

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-9">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script src = "https://cdn.socket.io/socket.io-3.0.0.js"></script>

</head>
<body>
test
</body>
</html>

<script>


const socket = io();
socket.emit('message', "hi server");
socket.on('message', text => {
    console.log("recieved: " + text);
    

});

</script>

I am not sure why this is happening and cant find any solutions, when i ran this locally it seemed to work fine. What can i do to fix it?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

172.18.0.1 is repl.it's HTML server internal address since they serve you the index.html and it's there where you make the call to const socket = io().

To see the client's real ip use socket.handshake.headers['x-forwarded-for'] expression as in:

  

  io.on('connection', (socket) => {
      io.emit('message', 'hello client')
      socket.on('message', (text) => {
        console.log(text)
        // console.log(socket.handshake.address) // Server serving index.html file IP address
    })

      console.log('a client connected')
      console.log('client IP addr: ' + socket.handshake.headers['x-forwarded-for']) // (REAL) client IP
    })

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda