I am wondering why you are able to initialize a normal javascript websocket with:
var socket = new WebSocket('ws://example.com/myprefix')
But with SockJS you need to call a normal http(s) url:
var sock = new SockJS('https://example.com/my_prefix');
Why doesn't SockJS allow 'normal' ws urls?
The websocket protocol, starting with ws:
is defined in RFC6455.
Whilst future implementations of the protocol may get dedicated port numbers for secure and non secure traffic, at the moment (2018) the protocol is initiated by making an HTTP request with an upgrade header to change protocol to WS. (Refs 1, 2 & 3).
Use of websockets and ws
protocols requires client support of the websockets API and server support of the protocol upgrade header and websocket communications.
SockJS is like a pollyfill for websockets that will fall-back to available communication channels if websockets are unavailable. Per the readme.md file,
"SockJS is intended to work for all modern browsers and in environments which don't support the WebSocket protocol -- for example, behind restrictive corporate proxies.
Both websockets and SockJS require support on the server. E.G. for node, either ws or SockJS for node. The readme.md file for SockJS state that the server will accept raw requests using the ws:
protocol header but doesn't mention on which port.
If the client side request doesn't accept ws
URL's I suggest assuming it was written around assumptions of making an initial HTTP/HTTPS request to determine support. Perhaps an update request might expand the protocols accepted in the URL!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With