hono icon indicating copy to clipboard operation
hono copied to clipboard

How can we obtain the client IP address?

Open jpforevers opened this issue 2 months ago • 8 comments

How can we obtain the client IP address?

We would like to use the IP address to perform an initial localization of the device, so obtaining the client IP is very important to us.

I have reviewed the Hono documentation but did not find a way to retrieve the client’s IP address.

I am a developer and I am more familiar with Hono’s MQTT Adapter.

To implement this feature in the MQTT Adapter, the following approaches seem feasible:

  1. Provide an option to enable the proxy protocol when creating the MQTT server

    MqttServerOptions mqttServerOptions = new MqttServerOptions();
    mqttServerOptions.setUseProxyProtocol(true);
    
  2. Retrieve the client IP during the client connection via mqttEndpoint.remoteAddress().host()

  3. Add a client IP field in the "Connection Event" (or some other mechanism?)

Is this right?

I’m not entirely sure how other adapters would implement this, but it should be similar.

Could this feature be added?

jpforevers avatar Oct 14 '25 10:10 jpforevers

Hi there, thanks for reaching out. Before going into the technical details, may I ask about the use case you are trying to address? Can you explain a little why and when you need to know about the client's IP address? Is it during initial provisioning of the device, or is it important to know the client IP every time the device connects to Hono? I am asking because if it is the former, then you might get away with simply adding the device's (precise) location when registering the device with the registry.

sophokles73 avatar Oct 17 '25 09:10 sophokles73

@sophokles73 Our requirement is to perform IP based positioning by obtaining the IP address of the device, but the device cannot know its own public network exit IP, the server knows. And we want to know the IP address when the device connects each time, not just the IP address during initial registration, because the IP address of the device can change.

jpforevers avatar Oct 21 '25 01:10 jpforevers

Ok, I see. I guess it would make sense to include the device's IP address as an additional property of downstream telemetry and event messages which originate from the device. Using the connection event would probably not work consistently across all protocol adapters because some of the supported transport protocol are connection-less. Would that work for you?

sophokles73 avatar Oct 21 '25 08:10 sophokles73

@sophokles73 Yes, you're right. Using connection events is indeed inappropriate. It's up to you to decide how Hono provides client IP addresses to the outside world. Thank you, this is great.

jpforevers avatar Oct 23 '25 01:10 jpforevers

Besides, I think you look a lot like Steve Jobs :) @sophokles73

jpforevers avatar Oct 23 '25 02:10 jpforevers

It’s been quite a while — has there been any progress on this?

If there’s a shortage of manpower, I might be able to help. I once fixed a bug for vertx-mqtt: https://github.com/vert-x3/vertx-mqtt/pull/252.

I’m fairly familiar with Vert.x and Quarkus.

Of course, it would be even better if you could provide some tips.

@sophokles73

jpforevers avatar Nov 05 '25 01:11 jpforevers

Oh, there seems to have been a misunderstanding. I do not have the bandwidth to implement this any time soon. So, if you need/want this to be in the next release, we would be glad to review/accept a PR for it.

However, as indicated above, I would prefer a solution that works for all protocol adapters.

Here's a rough overview of the steps involved:

  1. Define the property to be added to downstream messages in the Telemetry API specification documents. Remember to define the property to be optional and make its inclusion configurable by means of a common config property on the adapters. We generally try to make such features configurable at the tenant and adapter level, i.e. you can set the configuration at the tenant level but also for each adapter separately, with the latter overriding the former.
  2. Determine the right places in the adapters for acquiring the client IP and adding it to the properties of downstream messages, based on the tenant/adapter configuration.
  3. Add some integration tests validating the feature.

WDYT?

sophokles73 avatar Nov 05 '25 07:11 sophokles73

OK, it's good. I will do it.

jpforevers avatar Nov 05 '25 09:11 jpforevers