title: Interacting between new websocket(A) and new websocket.server(B)
body: <p>I am newbie to websockets.</p>
  <p>I need to build a script in nodejs, which is an interface between two sockets.
  wsHTML (act as server) communicates with an html screen and wsORG (act as client) communicates with an server.
  When event &quot;open&quot; of wsORG occurs, the script must send a message to wsHTML.
  I always get the error wsHTML not defined on command wsHTML.send.</p>
  <p>I simplified the problem. I wrote the below code using an open echo server from wersocket.org as wsORG, so it is easy to reproduce the error.</p>
  <p>The code I'm using is:</p>
  <pre><code>const WebSocket = require('ws');
  console.log(&quot;++++++++++++++++++++++++Script started++++++++++++++++&quot;);
  // ========================================= Websocket Server for HTML clients ==========================================
  const ws_server = new WebSocket.Server({ port: 81 });
  console.log(&quot;+++ws_server instantiated&quot;);
  
  ws_server.on('connection', function connection(wsHTML) {
      console.log('\r\nHTML client connected.');
      wsHTML.on('message', function incoming(message) {
          console.log('HTML client sent a command: [' + message + ']');
          if(message == &quot;PriceOf411111111111&quot;){
              f_SendMsgToORG(wsORG, &quot;Please, send me the Price of 411111111111&quot;);
          }else{
              console.log(&quot;Unexpected command received&quot;);
          }
      });
      wsHTML.on('close', function incoming(message) {
          console.log('ws_server Closed.');
      });
      wsHTML.on('error', function incoming(message) {
          console.log('ws_server ERROR ' + message );
      });
  });
  
  // ========================================= Websocket Cliente wsORG ==========================================
  var wsORG = new WebSocket('wss://echo.websocket.org/');
  console.log(&quot;+++WebsocketORG instantiated&quot;);
  
      wsORG.addEventListener('open', function () {
          console.log('\r\n\r\nCommunicaton with wsORG estabilished.');
      });
      wsORG.addEventListener('close', function () {
          WebSocketOpenConnection = false;
          console.log('Conection with WSorg was CLOSED');
      });
      wsORG.addEventListener('message', function (event) {
          var data = event.data;
          console.log(&quot;\r\nWSorg echoed: &quot; + data);
          f_SendMsgToHTML(p_Socket, (&quot;Send answer from wsORG to HTML client: &quot; + data))
      });
  
  function f_SendMsgToHTML(p_Socket, p_message){
      console.log(&quot;Send message to HTML client [&quot; + p_message + &quot;]&quot;);
      if (typeof(p_Socket) == &quot;undefined&quot;){
          console.log(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;OPS: Socket undefined&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);
          console.log(p_message);
      }else{
          console.log(&quot;Socket OK&quot;);
          p_Socket.send(p_message);
          console.log(&quot;Sent to HTML client [&quot; + p_message + &quot;]&quot;);
      }
  }
  
  function f_SendMsgToORG(p_Socket, p_message){
      if (typeof(p_Socket) == &quot;undefined&quot;){
          console.log(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;OPS: Socket undefined&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);
          console.log(p_message);
      }else{
          console.log(&quot;Socket OK&quot;);
          p_Socket.send(p_message);
          console.log(&quot;Sent to ORG [&quot; + p_message + &quot;]&quot;);
      }
  }
  </code></pre>
  <p>When I run the code, wait for WebsocketORG to be initialized and then connect with HTML client, the response is:</p>
  <pre><code># node doubt_stackoverflow.js
  ++++++++++++++++++++++++Script started++++++++++++++++
  +++ws_server instantiated
  +++WebsocketORG instantiated
  
  
  Communicaton with wsORG estabilished.
  
  HTML client connected.
  HTML client sent a command: [PriceOf411111111111]
  Socket OK
  Sent to ORG [Please, send me the Price of 411111111111]
  
  WSorg echoed: Please, send me the Price of 411111111111
  /usr/local/src/Foxbit/doubt_stackoverflow.js:39
                  f_SendMsgToHTML(p_Socket, (&quot;Send answer from wsORG to HTML client: &quot; + data))
                                  ^
  
  ReferenceError: **p_Socket is not defined**
      at WebSocket.&lt;anonymous&gt; (/usr/local/src/Foxbit/doubt_stackoverflow.js:39:19)
      at WebSocket.onMessage (/usr/local/src/Foxbit/node_modules/ws/lib/event-target.js:125:16)
      at WebSocket.emit (events.js:198:13)
      at Receiver.receiverOnMessage (/usr/local/src/Foxbit/node_modules/ws/lib/websocket.js:797:20)
      at Receiver.emit (events.js:198:13)
      at Receiver.dataMessage (/usr/local/src/Foxbit/node_modules/ws/lib/receiver.js:437:14)
      at Receiver.getData (/usr/local/src/Foxbit/node_modules/ws/lib/receiver.js:367:17)
      at Receiver.startLoop (/usr/local/src/Foxbit/node_modules/ws/lib/receiver.js:143:22)
      at Receiver._write (/usr/local/src/Foxbit/node_modules/ws/lib/receiver.js:78:10)
      at doWrite (_stream_writable.js:415:12)
  #
  </code></pre>
  <p>Note thet wsHTML is opened, receives a command, but as I need to answer to it, it gives says that the socket does not exist (probably it was closed, but not by me).</p>
  <p>I just have tried a lot but without success.</p>
  <p>The doubt is: How I reference wsHTML in wsORG?</p>
  <p>Best
  carliedu</p>