What are the potential pitfalls of using WebSockets for client-server communication?

One potential pitfall of using WebSockets for client-server communication is that it can be vulnerable to security risks such as cross-site scripting (XSS) attacks. To mitigate this risk, it is important to properly validate and sanitize user input before sending it over the WebSocket connection.

// Validate and sanitize user input before sending it over WebSocket
$user_input = $_POST['user_input'];
$clean_input = filter_var($user_input, FILTER_SANITIZE_STRING);

// Send sanitized input over WebSocket connection
$socket = new WebSocket("ws://localhost:8080");
$socket->send($clean_input);