The integration of the Internet of Things (IoT) with Wireless Sensor Networks (WSNs) typically involves multihop relaying combined with sophisticated signal processing to serve as an information provider for several applications such as smart grids, industrial, and search-and-rescue operations. These applications entail deploying many sensors in environments that are often random which motivated the study of beamforming using random geometric topologies. This paper introduces a new algorithm for the synthesis of several geometries of Collaborative Beamforming (CB) of virtual sensor antenna arrays with maximum mainlobe and minimum sidelobe levels (SLL) as well as null control using Canonical Swarm Optimization (CPSO) algorithm. The optimal beampattern is achieved by optimizing the current excitation weights for uniform and non-uniform interelement spacings based on the network connectivity of the virtual antenna arrays using a node selection scheme. As compared to conventional beamforming, convex optimization, Genetic Algorithm (GA), and Particle Swarm Optimization (PSO), the proposed CPSO achieves significant reduction in SLL, control of nulls, and increased gain in mainlobe directed towards the desired base station when the node selection technique is implemented with CB.