DEV Community

Cover image for Building PortClient: A Tool to Simplify and Speed Up Port Management
Vitalii
Vitalii

Posted on

Building PortClient: A Tool to Simplify and Speed Up Port Management

Building PortClient: A Tool to Simplify and Speed Up Port Management

Why I Decided to Build PortClient

As a Frontend Lead, managing development environments effectively is crucial for both personal productivity and team collaboration. In our day-to-day work, our team often needs to switch between different ports. It's vital to quickly check if a port is in use, whether there's a process listening on that port, or if we need to kill a process that's occupying a port. However, the existing solutions were frustratingly slow, especially on macOS. For example, when using the default lsof -i -P command, it could take up to 30 seconds to list all open ports, which was extremely annoying when we needed to quickly verify the status of a port.

This long wait time led us to the decision to build our own utility that would allow us to check port status and kill processes faster. We wanted something that would be fast, efficient, and easy to use - hence, PortClient was born.

The Goal: Simplifying Port Management and Speeding It Up

The main goal of PortClient was to make port management easier and faster. In a fast-paced development environment, speed is critical. If you're trying to kill or verify the status of multiple ports, waiting 30 seconds for each check can quickly become a time-consuming task. I wanted to create a tool that would allow us to:

  • Check if a port is open and verify if a process is listening on it - instantly.
  • Kill a process that's using a specific port without any delay.
  • Improve speed significantly compared to default tools like lsof.

Our key focus was not just to make port management functional but to optimize performance, making it fast enough for real-time tasks.

The Challenges I Faced

1. Slow Default Command on macOS

The default lsof -i -P command in macOS provides a list of open ports, but it is quite slow - taking about 20-30 seconds. This delay was unacceptable when we needed to quickly check port status or terminate processes. After trying several alternatives, we found a solution: we could optimize the command by adding the -n flag, which avoids hostname lookups and significantly reduces the processing time. The optimized command, lsof -i -P -n, was faster, but there was still room for improvement.

2. Further Optimization

Even with lsof -i -P -n, it still wasn't fast enough for our needs. After experimenting, we realized that using lsof -i :${this.ports} would give us exactly the information we needed for a specific port without listing all ports. This tweak cut the process time from 20 seconds to 1 second, which was a huge improvement.

lsof -i :${this.ports}
Enter fullscreen mode Exit fullscreen mode

This command became the core of PortClient, making it lightning fast for checking whether a specific port is in use.

3. Command-Line Interface (CLI) for Ease of Use

One of the biggest challenges was making sure the tool was easy to use, especially for developers who might not want to install anything locally. We decided to build a CLI utility that could be executed directly from the terminal using npx - meaning developers wouldn't need to install anything to use it.

For example, with PortClient, you can run:

npx port-client 3000
Enter fullscreen mode Exit fullscreen mode

This command will immediately check if port 3000 is open and show the relevant details, without requiring any installation on the user's machine.

To achieve this, we used the "bin" field in the package.json file to register our CLI command:

{
  "bin": {
    "port-client": "cli.js"
  }
}
Enter fullscreen mode Exit fullscreen mode

This method allows users to run our tool without needing to install it globally, making it much more convenient for quick, one-off usage.

How PortClient Works

PortClient checks the status of a port, identifies whether any process is listening on it, and offers the option to terminate any process that's using the port.

1. Check Port Status

If you need to check whether a port is in use, simply use:

npx port-client 3000
Enter fullscreen mode Exit fullscreen mode

This will tell you if the port is active or not, and if it is, which process is using it.

2. Kill a Process Using a Port

If you find that a port is in use and you need to kill the process, you can execute:

npx port-client kill 3000
Enter fullscreen mode Exit fullscreen mode

This command will attempt to terminate the process that is listening on port 3000, freeing the port for your use.

Conclusion: Lessons Learned and Future Improvements

Building PortClient has been a valuable learning experience. Here's what I learned during the process:

  • Speed is Crucial: Optimizing existing commands like lsof can yield significant improvements in speed, making a tool much more efficient.
  • Ease of Use Matters: Building a command-line utility that's easy to run with npx can help streamline development workflows, reducing friction and making tools accessible to everyone on the team.
  • Cross-Platform Considerations: While the macOS solution works great, a Windows or Linux version would need further platform-specific tweaks to handle port management.

In the future, I plan to:

  • Enhance error handling and edge cases.
  • Add more features, like selecting multiple ports interactively.
  • Consider building a GUI or web interface for users who prefer a graphical approach.

If you're interested in checking out the code or contributing, feel free to visit the GitHub repository.

PortClient is now a part of our development toolkit, making port management much faster and more efficient. I hope this tool can save you time and frustration in your development journey!

🌟 Want to Learn More?

👉 𝗠𝗲𝗱𝗶𝘂𝗺 - https://medium.com/@vitaliisemianchuk

👉 𝗟𝗶𝗻𝗸𝗲𝗱𝗶𝗻 - https://www.linkedin.com/in/vitalii-semianchuk-9812a786/

👉 Telegram - https://t.me/jsmentorfree

Top comments (0)