Is the WebSocket constructor native Javascript?


Is the WebSocket constructor native Javascript?



I'm wondering, is the WebSocket() constructor a part of native Javascript? Or is it a part of the "ws" module of node.js?



If it's a part of the "ws" module, I'm confused, because I'm in a lynda.com training course, and this is a client-side js file that me and my instructor set up:


var ws = new WebSocket("ws://localhost:3000");
ws.onopen = function() {
setTitle("Connected to Cyber Chat");
};

ws.onclose = function() {
setTitle("DISCONNECTED");
};

ws.onmessage = function(payload) {
printMessage(payload.data);
};

document.forms[0].onsubmit = function () {
var input = document.getElementById('message')
ws.send(input.value);
input.value = '';
};

function setTitle(title) {
document.querySelector('h1').innerHTML = title;
}

function printMessage(message) {
var p = document.createElement('p');
p.innerText = message;
document.querySelector('div.messages').appendChild(p);
}



So in my mind, the WebSocket() constructor would have to be a native Javascript constructor that looks for a Websocket server in the url you provide it in the argument, because there is nothing referring this file to the "ws" node module that I have in this project folder.





Yes, it's part of "native" javascript. More information about this interface can be found here.
– Adriani6
Jun 30 at 0:46





Its not the same code base, but the API is the same, rejectUnauthorized wont work in the browser though, so if invalid/self-signed SSL certs wont work for wss, apart from that their pretty much identical.
– Lawrence Cherone
Jun 30 at 0:52



rejectUnauthorized





NIT: WebSocket is not part of "native JavaScript" (ECMAScript). It is part of 'standard browser Web API' with some "ports" in/for node.js. A valid ECMAScript ("JavaScript") implementation does not need to provide this type.
– user2864740
Jun 30 at 0:54





1 Answer
1



webSocket is not part of the Javascript ECMA standard. It's not a standard part of Javascript itself. You will find no mention of it at all in the EMCAScript standard that defines the Javascript language.



It has its own standard for the webSocket protocol and then an implementation that meets that standard is supplied by browsers and by the ws library (and other libraries too). The API used by the browsers is being worked on by the W3C in a standards process, but that is completely independent of Javascript itself.



But, if you were to grab a plain JS engine such as the V8 engine, it would not have a webSocket implementation in the V8 engine. That's why, for example, node.js does not come with a webSocket implementation built-in. It's not part of the V8 Javascript engine.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV