The Internet of Things (IoT) makes IoT devices more vulnerable to cyberattacks, especially Distributed Denial of Service (DDoS), raising privacy and security issues. Application-layer DDoS: zombie machines submit queries to the affected server. IDS cannot detect these requests because TCP connections are valid. In this research, we propose the Lion-Salp-Swarm-Optimization Algorithm (LSSOA), which utilizes the freely available IoT-Flock software to create a dataset containing both legitimate and malicious traffic. We employ metaheuristic algorithms like Lion optimization, Whale optimization, Spider-Monkey optimization, and Salp Swarm optimization to detect online attacks and defend the Internet of Medical Things (IoMT) environment. Our framework accurately detects attacks while reducing false positives by overcoming intrusion detection restrictions. Our metaheuristic algorithm outperforms others. Our method is useful for Internet of Things-based enterprises. Overall, the LSSOA framework provides a powerful tool to detect and prevent cyber-attacks in the IoMT environment, demonstrating the potential benefits of novel intrusion detection techniques. Our study emphasizes the importance of enhancing IoT security, particularly in critical industries like healthcare, and highlights the need for continuous efforts to develop effective and innovative approaches to address emerging cyber threats.