Showing posts with label Web socket. Show all posts
Showing posts with label Web socket. Show all posts

Friday, 15 September 2017

Web sockets online Tutorial - Page2

Web sockets online Tutorial - Page2

Question: Difference between socket and websocket?
WebSockets run from browsers and connecting to Server over a similar to HTTP. it require a permanent connection to its server.
Sockets are more powerful and generic and not restricted to http protocol OR browser.

Question: How to detect the message type in onmessage event?
socket.onmessage = function(event){
   if(typeOf === String ) {
      console.log("Received data string");

Question: How does Web Socket support Single TCP Connection?
In Web a new TCP connection is initiated for a each HTTP request and terminated after the response is received which means A new TCP connection need for each HTTP request. Whereas In WebSocket, the HTTP connection is upgraded and client and server communicate over that same TCP connection.

Question: How Web Socket support on Bi-directional?
In Web client(like browser) only can send the request to server then server send the response to the same client only.
Whereas In WebSocket, there is no pattern like request/response and Both (Client/Server) can send message to other party.

Question: How Web Socket support on Full Duplex?
In Web client(like browser) only can send the request to server then server send the response to the same client only. At a given time, either client is talking to server or server is talking to client. Whereas In WebSocket they can talk each other.

Question: What is the WebSocket API?
The WebSocket API is the asynchronous communication from client to server and it takes place over single TCP socket using the ws (unsecure) or wss (secure) protocol and can be used by any client or server application.

Question: How can we secure WebSocket API?
Normal Web Socket API start with ws but secure API start with wss.

Question: What is Ajax Long-Polling?
  1. A client requests a webpage using HTTP.
  2. The server does not immediately respond with the requested information but waits until there's new information available.
  3. When there's new information available, the server responds with the new information.
  4. The client receives the new information and immediately sends another request to the server, re-starting the process.

Question: What is Comet in HTML5?
Comet is a collection of techniques prior to HTML5 which use streaming and long-polling to achieve real time applications.

Question: What is Server Sent Events?
Server Sent Events (SSE) connections can only push data to the browser.
online stock quotes, or twitters updating timeline or feed are good examples of an application that could benefit from SSE.

Question: How to add http headers in Websockets client API?
We can not add custom header in Web sockets.

Question: How can we pass auth while calling Connecting WebSocket?
 var ws = new WebSocket("ws://");

Question: Is PHP support for Web Sockets available?
Yes, for this you need install the library.
Apache Module:
PHP WebSocket:

Wednesday, 13 September 2017

Web sockets online Tutorial

Web sockets online Tutorial

Question: What is WEBRTC?
WebRTC is an API that support browser-to-browser applications for voice calling, video calling, and P2P file sharing.

Question: What is Web Socket?
WebSocket is a protocol, providing full-duplex communication channels over a single TCP connection.

Question: How does WebSocket work?
WebSockets provide a persistent connection between a client and server that both parties can use to start sending data at any time.
The client establishes a WebSocket connection through a process known as WebSocket handshake.

Question: Why should I use WebSockets?
Because the server can actively push information to the client any time.
And the client can push information to the server without HTTP requests.
WebSocket is designed for low protocol and wire-level overhead - chunky HTTP headers.

Question: What is difference between WebRTC vs Websockets?
WebRTC is designed for high-performance, high quality communication of video, audio and arbitrary data (Browser to Browser).
WebSocket is designed for bi-directional communication between client and server (Browser to Server and vice versa).

Question: Importance of Websockets?
  1. Web Socket is an independent TCP-based protocol but support other protocol.
  2. Web Socket is a transport layer on top.
  3. Define sub-protocols for example XMPP, STOMP, and AMQP.
  4. Used JavaScript that can interpret the Web Socket handshake, establish and maintain a Web Socket connection.

Question: How websocket established the connection ?
  1. The client establishes a connection through Web Socket handshake.
  2. Client sending a HTTP request to the server.
  3. Header is also sent from which server can get the details

Question: Give example of Web Socket connection?
//Declare variables
var output;
var wsUri = "ws://";

//Defination of init function
function init() {
    output = document.getElementById("output");
function testWebSocket() {
    websocket = new WebSocket(wsUri);
    websocket.onopen = function(evt) {

function onOpen(evt) {
    output.innerHTML = 'Connected';

//Call on load
window.addEventListener("load", init, false);

ws:// Here ws is used for Web socket.
It is public URL you can use it for testing.
After testing, you need to create your own because you can't use it in your production OR development server.

Question: How to send error message in Web Socket?
    var wsUri = "ws://";
    var output;

    /* Init the WebSocket */          
    function init() {
       output = document.getElementById("output");

    /* Declare the Socket Function */          
    function startWebSocket() {
       websocket = new WebSocket(wsUri);

       websocket.onopen = function(evt) {

       websocket.onclose = function(evt) {

       websocket.onerror = function(evt) {

    /* Function Defination */                  
    function onOpen(evt) {
       writeToScreen("Web Socket Connected");
       doSend("This is custom message");

    function onClose(evt) {
       writeToScreen("Web Socket Dis Connected");

    function onError(evt) {
       writeToScreen('Error :' +;

    function doSend(message) {
       writeToScreen("Sent Data: " + message); 


    function writeToScreen(message) {
       var pre = document.createElement("div");          
       pre.innerHTML = message; output.appendChild(pre);

    window.addEventListener("load", init, false);