Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

node.js - should socket.get work on client?

node.js - should socket.get work on client?

Problem

I'm new to node.js and I'm making a simple chat app to get started. I'm a bit confused with the socket.set and socket.get methods.

The way the app works, is - first the client sets its username (pseudo):

function setPseudo() {
   if ($("#pseudoInput").val() != "")
   {
      socket.emit('setPseudo', $("#pseudoInput").val());
      $('#chatControls').show();
      $('#pseudoInput').hide();
      $('#pseudoSet').hide();
   }
}

On the Server, the pseudo is fetched with:

socket.on('setPseudo', function (data) {
   socket.set('pseudo', data);
  });

If I understand correctly, the server sets the pseudo variable with data received from this particular client. The server can later get that variable with socket.get. The following code broadcasts a message from a client to all clients:

socket.on('message', function (message) {
   socket.get('pseudo', function (error, name) {
      var data = { 'message' : message, pseudo : name };
      socket.broadcast.emit('message', data);
      console.log("user " + name + " send this : " + message);
   })
  });

What I don't understand is, why can't the client itself use socket.get to fetch its own pseudo? Using socket.get('pseudo') gives an error saying socket.get is not a function. Or am I overcomplicating this, and it would be better to just store the pseudo in a hidden field on the client or something similar? It just feels strange that a client should have to get its own username from the server.

EDIT:

Upon clicking Send, this code displays the sent message on the client itself. However, the displayed username is "Me". How can I modify it to show the client's username from the server?

addMessage($('#messageInput').val(), "Me", new Date().toISOString(), true);
function addMessage(msg, pseudo) {
   $("#chatEntries").append('

' + pseudo + ' : ' + msg + '

'); }
Problem courtesy of: jovan

Solution

You have to realize that although socket is a name used by both the server and the client, and interfaces are similar these are two independent things. (i.e. server socket and client socket) describing two ends of one connection.

If server sets some data on a socket what it actually does is it saves some data in its own memory and remembers that this data is associated with the socket. So how would client read something from server's memory? How can machine A read data from machine's B memory? Well, the only (reasonable) possibility is to send that data over network and this is actually what happens.

As for the other question: it's actually natural for the client to get its own name from the server or at least validate that name. Consider this scenario: two clients connect to the server and use the same name. This would lead to a conflict so it is up to the server to solve the problem. Basically you would tell one of the clients "sorry, this name is already being used, use something else".

Solution courtesy of: freakish

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

node.js - should socket.get work on client?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×