TL;DR
You need to tell your WebSocket client to trust a self-signed certificate if the server is using one. This usually involves adding the certificate to your trusted store or configuring the client to ignore certificate validation (not recommended for production).
Solution Guide
- Understand the Problem: WebSockets, like HTTPS, rely on certificates to verify the identity of the server. If you’re using a self-signed certificate (one not issued by a recognised Certificate Authority), your client will rightly complain because it can’t automatically trust it.
- Identify Your Client Library/Tool: The method for trusting the certificate depends on what you’re using to connect to the WebSocket server. Common examples include:
- Python (
websocketslibrary) - JavaScript (Browser or Node.js)
- Command-line tools like
wscat
- Python (
- Option 1: Add the Certificate to Your Trusted Store (Recommended): This is the most secure approach.
- Get the Certificate File: Obtain the certificate file (.crt, .pem) from your server administrator.
- Add to System Trust Store: The process varies by operating system:
- Linux (Debian/Ubuntu): Copy the certificate to
/usr/local/share/ca-certificates/and runsudo update-ca-certificates.
- macOS: Open Keychain Access, import the certificate, and mark it as trusted for all applications.
- Windows: Double-click the certificate file, follow the wizard to install it into the Trusted Root Certification Authorities store.
- Linux (Debian/Ubuntu): Copy the certificate to
- Option 2: Configure Your Client (Less Secure – Use with Caution): This bypasses certificate validation and should only be used for testing or in controlled environments.
- Python (
websockets): Thewebsocketslibrary doesn’t directly offer a way to ignore certificate errors. You typically need to use the underlying SSL context:import ssl context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE ws = websocket.connect('wss://your-server', ssl_context=context) - JavaScript (Node.js –
wslibrary):const ws = new WebSocket('wss://your-server', { rejectUnauthorized: false });Warning: Setting
rejectUnauthorized: falsedisables certificate validation, making your connection vulnerable to man-in-the-middle attacks. Do not use this in production. - Command-line (
wscat): Use the--no-check-certificateflag:wscat --no-check-certificate wss://your-server
- Python (
- Restart Your Client: After adding the certificate to your trusted store or configuring your client, restart it to ensure the changes take effect.
- Test the Connection: Attempt to connect to your WebSocket server again. The connection should now succeed without certificate errors.
Important Security Note: Bypassing certificate validation significantly reduces security. Always prefer adding the certificate to your trusted store whenever possible.

