websocket wss code example

Example 1: javascript websocket example code

var Socket = new WebSocket('ws://' + window.location.hostname + ':81/'); // The '81' here is the Port where the WebSocket server will communicate with
// The instance of the WebSocket() class (i.e. Socket here), must need to be globally defined

Socket.send("pass your data here, and it'll be String"); // This method one can call locally

Example 2: js connect to websocket

var exampleSocket = new WebSocket("wss://www.example.com/socketserver", "protocolOne");

Example 3: what are websockets

persistent bi-directional communication channel between 
a client (e.g. a browser) and a backend service. 
In contrast with HTTP request/response connections, websockets 
can transport any number of protocols
provide  message delivery without polling
without racy, high-latency, and bandwidth-intensive implementations
establish TCP-style connections in a browser-compatible 
fashion using HTTP during initial setup.
Messages over websockets can be provided in any protocol, 
remove unnecessary overhead of HTTP requests and responses 
(including headers, cookies, and other artifacts).