Server side validation with socket.io

I just want to validate some data with nodejs and socket.io server side.

So I am sending data with socket.emit('validate data', data)

My approach still is to do it by posting the response back to the server side like this ...

socket.emit('validated data', boolean })

.. and get a client-side response like this:

socket.on('validated data', function (boolean) {
    validationResponse = boolean;
});

      

It seems very inconvenient and ineffective for logical purposes only . I think this is a very general question about communication between server and client with sockets, so an explanation would be very helpful.

Is there a better solution for getting a straight forward boolean or tiny data response?

+3


source to share


3 answers


My question was for shorter or more secure code, but as I suspected:

I think this is a very general question about server-client communication

.. there was a mistake in thinking about the connection on my side. The problem is that security is not better if you are trying to protect your application with such processes, because your client may have already falsified the return boolean on the client side.



So anyway, I prefer to validate it, but if the user sends a message with an invalid / accepted username, the message will not be sent to other clients, and the client itself will receive a message like username already taken

.

buddys, sorry and thanks for your answers. Special thanks to @ThiefMaster

0


source


How about a regex to validate your field data? You can store these regular expressions in an attribute on each element, applying them across the input class. Run these checks for events like change

or blur

and you're good to go ...

<input type="text" regex="(a|b)" />

<input type="text" class="regex typeA" />

      

jQuery and other JavaScript libraries have good form validation plugins, but you can also build them yourself:



$('.regex').each(function(el) {
  if ($(el).hasClass('typeA')) {
    // doValidation
  }
});

      

If you need to use your server to validate user input, there is no way to use WebSockets or AJAX requests to validate. Of course, WebSockets produces less data traffic ...

0


source


My guess is no, in fact there is no better (i.e. more suspicious) solution in general. I personally find your code to be pretty short. If you need shorter code, my only recommendation is to use shorter identifier names.

If your server code only performs one type of operation, you can eliminate your events and just use socket.send(data)

with socket.on("message", fn)

instead socket.emit

.

0


source







All Articles