Who is listening on a given TCP port on Mac?

In MacOS, we can use lsof command to find out who is listening to a TCP port. The following command will show the list of processes that are listening to TCP ports.

sudo lsof -i -P | grep LISTEN

// Output
mysqld       91         _mysql   28u  IPv6 0xeb2544dc938f7a27      0t0    TCP *:3306 (LISTEN)
mysqld       91         _mysql   31u  IPv6 0xeb2544dc938f7fe7      0t0    TCP *:33060 (LISTEN)
idea        363         beroza  259u  IPv4 0xeb2544dcae65e927      0t0    TCP localhost:6942 (LISTEN)
idea        363         beroza  748u  IPv4 0xeb2544dcb187aca7      0t0    TCP localhost:63342 (LISTEN)
mongod     1218         beroza   10u  IPv4 0xeb2544dcb14ffca7      0t0    TCP localhost:27017 (LISTEN)
Messenger 15978         beroza   89u  IPv4 0xeb2544dcbbab5927      0t0    TCP localhost:3103 (LISTEN)
Reference: