multiple websockets connections on the same client - server cannot handle

Let me clarify, this is kind of complicated. I'm implementing a form to insert data in the database.

I have two websockets connections in the same client side, connecting on the same nodejs server.

One connection is triggered after the user inserts a name on the "name" textfield of the form. Sends the data to the server, server checks the database if the name already exists and responces back "This already exists. Mayde you are inserting something that is already there".

The other connection is triggered if all the fields of the form are not blank and sends the data to server to insert them in the database.

I thought it was a good idea to distinguish on server-side ,the different connections using arrays. If the first element of the array is "name" call the checkName function, or if it is "insert" , call the insertInDB function.

I created two small testing files. They do not work. Connections are open and the client sends the data. I get no errors inte server nor the client side. But server never responces. I dont get the expected numbers, back in the client side. I dont think this is the right anyway. This is complecated, I hope the code helps you.

Is it possible, what I am trying to do? Any hints or alternatives?

Thanks

the code.... server-side

function WebSocketTest1(){
 var a=1;
 var b=2;
 var c = [a,b];

     var so = new WebSocket("ws://localhost:1337");

      so.onerror=function (evt) 
     {message.textContent = evt;}

     so.onopen = function(){
      message.textContent = "opened";
     so.send(c);
      message.textContent = "sended";
     }


     so.onmessage = function (evt) { 
        var received_msg = evt.data;
       document.getElementById("message").innerHTML=received_msg;
     }
}

function WebSocketTest2(){
 var d=3;
 var e=4;
 var f = [d,e];

     var sa = new WebSocket("ws://localhost:1337");

      sa.onerror=function (evt) 
     {message2.textContent = evt;}


     sa.onopen = function(){
     message2.textContent = "opened";
     sa.send(f);
     message2.textContent = "sended";
     }


     sa.onmessage = function (evt) {
         var received_msg = evt.data;
         document.getElementById("message2").innerHTML=received_msg;
}
}
</script>
</head>
 <input type="button" value="one" onClick="WebSocketTest1()"><br/>
  <input type="button" value="two" onClick="WebSocketTest2()"><br/>
<body>
  <div id="message"></div>
mesage2</br>
    <div id="message2"></div>
</body>
</html>

on the server side I am listing the sessions, to communicate only with a specific session, code found here and the server side (snippets)

    var connections = {};
    var connectionIDCounter = 0;
    var connection = request.accept(null, request.origin);

   // Store a reference to the connection using an incrementing ID
    connection.id = connectionIDCounter ++;
    connections[connection.id] = connection;


    console.log((new Date()) + ' Connection accepted.');

   connection.on('message', function(message) {             
   var ja=message;
   if(ja[0]==1)
  {ja[1]=7;}        

  else if(ja[0]==3)
  {ja[1]=8;}
   }); 



    connection.on('close', function(reasonCode, description) {
    console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    delete connections[connection.id];
    });
   });

// Send a message to a connection by its connectionID
function sendToConnectionId(connectionID, data) {
    var connection = connections[connectionID];
    if (connection && connection.connected) {
        connection.send(ja[1]);   
    }

I wonder why you need 2 connections. Open one connection and send the payload as JSON.

e.g.

var ws = new WebSocket("ws://localhost:1336");
ws.send(JSON.stringify({ command: "checkname", params: "xxxx"; });
ws.send(JSON.stringify({ command: "submit", params: { ... });

At the server side, you just have to parse the payload and determine which command is executed.

I am just wondering why you need websocket. It seems that you need to validate user presence in the application. If user is not present then you need to insert in the database else throw error.

you can go through jquery post and jquery form validation http://api.jquery.com/jQuery.post/