You need to provide a list of protocols as part of the object constructor:
var ws = new WebSocket(uri, ['binary', 'base64']);
If you use the websock.js library included with websockify, it will handle this for you. However, note that websock.js does not provide a standard WebSocket API, but rather a streaming oriented API. Even if you are using a raw WebSocket connection with websockify, note that you will still need to perform message recovery, because regular TCP does not have a message concept, so messages related to onmessage will be essentially "arbitrary".
source share