The Internet of Vehicles (IoV) has evolved from the classic Vehicular Ad-hoc NETworks (VANETs) as a result of the emergence of the Internet of Things (IoT). IoV is used for communication among vehicles in real-time with their drivers, other vehicles, pedestrians, fleet management systems, and roadside infrastructure. High vehicular speeds and frequent network topology changes make vehicle communication extremely difficult on the IoV network. More constraints are imposed on IoV communication performance in a huge network environment due to the difficult road conditions and the enormous quantity of vehicles. A promising approach to improve the IoV communication performance is through vehicle clustering. Minimizing the number of clusters and identifying a reliable Cluster head (CH) are some of the most challenging tasks. In this paper, we propose a Swarm optimization-based and mobility-aware clustering method termed SOMACA. SOMACA consists of two phases clustering phase and the routing phase. During the clustering phase, we combine mobility measures and cluster distance to generate the minimum number of clusters having stable CHs and employ the Sparrow Search algorithm (SSA) for CH selection. The routing phase consists of two steps (1) Route Formation and (2) Route Upkeep. The main target for route formation step is to build a secure routing path between IoV nodes and base station (BS) by establishing an optimal list of links that are ordered from high to low, and in each round, it selects the best one. Moreover, the Upkeep step aims to update and maintain the existing connection. The performance of SOMACA is assessed using simulation experiments with various metrics including average cluster lifetime, transmission range, and network grid size. The simulation results show that SOMACA reduces the average number of clusters by 42%, 48%, 47%, 9%, 22%,31%, 16%, and 43% less than CAVDO, GOA, GWOCNET, MFCA-IOV, MOGA-AWCP, HHOCNET, AMONE, and p-WOA algorithms respectively when the transmission ranges vary between 200 and 500. Moreover, SOMACA increases the average network lifetime by 11%, 12%, 9%, 6%, 7%, 9%, 3%, and 13% longer than the mentioned above compared algorithms, respectively.INDEX TERMS Clustering, the Internet of Things, the Internet of Vehicles, swarm intelligence, sparrow search algorithm, routing data, VANET.The associate editor coordinating the review of this manuscript and approving it for publication was Chan Hwang See.