Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket vs SockJS object

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?

like image 670
David542 Avatar asked Oct 10 '18 02:10

David542


1 Answers

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!

like image 62
traktor Avatar answered Oct 10 '22 23:10

traktor