Kafka producer produce data to topic from PORT
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When people say they want a Kafka producer to publish "from a port," they usually mean one of two things: either the producer should connect to a broker at a specific host:port, or the producer should read data arriving on some local socket and forward it into Kafka. Those are different problems.
For Kafka itself, the important configuration is the broker address in bootstrap.servers. A producer does not bind its own special source port in normal application code. It connects to the broker listener that Kafka exposes.
Point the Producer at the Broker host:port
The basic producer configuration is just a list of broker endpoints. If your broker listens on 9093, that goes in bootstrap.servers.
The producer connects to whatever listener the broker exposes. If the broker is reachable on port 9093, the producer must use that address. If the broker listens on the default 9092, use that instead.
Configure the Broker Listener Correctly
The producer side only works if the broker is actually listening on the port you think it is. A minimal broker configuration might look like this:
listeners tells Kafka which port to bind. advertised.listeners tells clients what address to use after the initial bootstrap step. If those values are wrong, producers often connect once and then fail on metadata updates.
That is why "produce to Kafka on a port" is really a broker-listener question first and a producer-configuration question second.
Forward Data That Arrives on Another Port
If your real problem is "data arrives on some TCP port and I want to publish it to Kafka," then Kafka is only one half of the flow. Your application also needs to listen on that source port.
Here is a minimal Python example that accepts one TCP connection and forwards each received line to Kafka:
Now there are two ports involved:
- '
5000for incoming socket data' - '
9093for the Kafka broker connection'
That distinction is often the missing piece in questions like this.
Common Pitfalls
The biggest mistake is assuming the Kafka producer itself should "produce from port 9093." In normal usage, the producer connects to the broker listener at that port. It does not usually own that port.
Another common issue is configuring listeners but not advertised.listeners correctly on the broker. That frequently breaks client connections in containerized or remote setups.
It is also easy to confuse the source port of incoming application data with the destination port of the Kafka broker. Those are separate network roles.
Finally, remember to flush or close the producer before exit. Otherwise, the last messages may still be buffered.
Summary
- A Kafka producer normally connects to the broker at a configured
host:port. - Set that address with
bootstrap.servers. - Make sure the broker
listenersandadvertised.listenersmatch reality. - If data arrives on another port first, your app must listen there and then forward to Kafka.
- Distinguish the source socket port from the Kafka broker port.

