Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] How to get client ip address in Websocket mode #886

Closed
vttkrk opened this issue Aug 31, 2023 · 1 comment
Closed

[question] How to get client ip address in Websocket mode #886

vttkrk opened this issue Aug 31, 2023 · 1 comment
Labels

Comments

@vttkrk
Copy link

vttkrk commented Aug 31, 2023

I created a aedes server with the code below

const aedes = require("aedes")();
const { createServer } = require("aedes-server-factory");
const port = 8000;
const httpServer = createServer(aedes, { ws: true, trustProxy: false });
httpServer.listen(port, function () { console.log("Server started"} );

In preConnect handler

aedes.preConnect = (client, packet, callback) => {
  console.log(
    "client remoteAddress " + client.connDetails.ipAddress
  );
  callback(null, true);
};

I wonder is 'client.connDetails.ipAddress' the right way to get the client address.
In simple MQTT example I can get the client remote address with 'client.conn.remoteAddress' but it isn't work in websocket mode. I try to console.log(client) and found out that client.connDetails.ipAddress holds value "::1"
I think because the server is running on my local machine. But I can test if it work in production environment.

Please suggest some correct way to get the client IP address.
Thanks!

@robertsLando
Copy link
Member

I wonder is 'client.connDetails.ipAddress' the right way to get the client address.

It depends on if the client connects using websockets or tcp socket, anyway everything parsed is placed in connDetails as you can see in source: https://github.com/moscajs/aedes-server-factory/blob/main/index.js#L82C9-L82C20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants