'Error: write EPROTO 4455222784:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:332:

I have a websocket server running on the backend using the ws nodejs library. I'm getting this error whenever I am trying to connect to it from the client side.

events.js:377
      throw er; // Unhandled 'error' event
      ^

Error: write EPROTO 4455222784:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:332:

    at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:94:16)
Emitted 'error' event on WebSocket instance at:
    at ClientRequest.<anonymous> (/Volumes/lilac/Projects/Projects/5. Fireplace/fireplace/node_modules/ws/lib/websocket.js:718:15)
    at ClientRequest.emit (events.js:400:28)
    at TLSSocket.socketErrorListener (_http_client.js:475:9)
    at TLSSocket.emit (events.js:400:28)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  errno: -100,
  code: 'EPROTO',
  syscall: 'write'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: `next dev`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/debabrata/.npm/_logs/2021-08-04T07_56_05_447Z-debug.log

Here's the code running on the server:

import { createServer } from "http"                                   
import { WebSocketServer } from "ws"                                  

const server = createServer()                                         
const wss = new WebSocketServer({ server })                           
                                                  
                                                                      
server.listen("8080", () => {                                         
    console.log("Server listening on port 8080")                      
})                                                                    
                                                                      
wss.on("connection", (ws, req) => {                                   
    ws.on("open", () => console.log("connection open"))               
    ws.on("close", () => console.log("connection closed"))                                                                       
})

Client side code (Next.js)

import WebSocket from 'isomorphic-ws'                                   
import {useEffect} from 'react'                                         
                                                                         
const ws = new WebSocket("wss://localhost:8080/")                       
                                                                        
const Page = () => {                                                   
                                                                        
    useEffect(() => {                                                   
        ws.onopen = () => {                                             
            console.log("connection open")                              
        }                                                               
    })                                                                  
                                                                       
    return (                                                            
        <div>                                                           
        </div>                                                          
    )                                                                   
}                                                                       
export default Page

I should also mention that I had to update npm and node versions while building this. Is that causing a problem? How do I resolve this issue?



Solution 1:[1]

I looked back again. It works if I connect to ws://localhost:808

Solution 2:[2]

Your localhost URL works with http only unless you get a TLS certificate signed by an CA (certificate authority) that endorse your localhost connection. If you haven't done that wss won't work with http://localhost:{port} since:

  • wss connects on https only
  • ws connects on http

and vice-versa:

  • https accepts wss only
  • http accepts ws only

as well described here: Difference between ws and wss?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Debabrata Mondal
Solution 2 LET1S3N