title: Deserializing UTF8 encoded Byte[] in javascript in the browser or in the node.js application?
body: <p>I have an event class in c# :</p>
  
  <pre><code>public class AreaInterventoCreata{
      //public properties
  }
  var message= new AreaInterventoCreata();
  </code></pre>
  
  <p>I create an instance of this class server side. My aim then is to communicate this creation to the clients that have subscribed to this kind of event.</p>
  
  <p>Therefore I am passing it to my broker RabbitMQ the follwing <code>byte[]</code> :</p>
  
  <pre><code>var responseBody = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message))
  responseChannel.BasicPublish(
                               "",                 // exchange 
                               properties.ReplyTo, // routingKey
                               responseProperties, // basicProperties 
                               responseBody);      // body
  </code></pre>
  
  <p>Some node.js server has subscribed to this kind message  :</p>
  
  <pre><code>q.subscribe(function (message) { 
      for (var i = 0; i &lt; sockets.length; i++) {
          console.log('_sockets[' + i + '] emitted');
          sockets[i].emit(event, message);
      }
  });
  </code></pre>
  
  <p>This node.js has himself received socket.io connection coming from browsers, and therefore can perform a push to these sockets, eg these browsers :</p>
  
  <pre><code>sockets[i].emit(event, message);
  </code></pre>
  
  <p>Finally I receive this message in my browser :</p>
  
  <pre><code>var socket = io.connect('http://localhost:8091');
  socket.on('Events_AreaIntervento_AreaInterventoCreata:Events', function (data) {
      var json = Utf8.decode(data);
  });
  </code></pre>
  
  <p>When I inspect data trough firebug , it is an object data with an array of number define as <code>data.data</code>.</p>
  
  <p>I assumed this array of number to be the array of byte i have given at the beginning of the process. Am I wrong in this assumption ?</p>
  
  <ul>
  <li><em>How could I deal with it so that I can work with some normal object in javascript?<strong></em>(found it!)</strong></li>
  <li>Should I translate it in json directly at the node.js server level? is it more efficient? more something to do so?</li>
  </ul>
  
  <p>ok found it (at least part of it) with the help of this <a href="http://www.webtoolkit.info/javascript-utf8.html" rel="nofollow">website</a></p>
  
  <pre><code>var Utf8 = {
      // public method for url decoding
      decodeArray: function (utfArray) {
          var string = "";
          var i = 0;
          var c = c1 = c2 = 0;
  
          while (i &lt; utfArray.length) {
  
              c = utfArray[i];
  
              if (c &lt; 128) {
                  string += String.fromCharCode(c);
                  i++;
              }
              else if ((c &gt; 191) &amp;&amp; (c &lt; 224)) {
                  c2 = utfArray[i + 1];
                  string += String.fromCharCode(((c &amp; 31) &lt;&lt; 6) | (c2 &amp; 63));
                  i += 2;
              }
              else {
                  c2 = utfArray[i + 1];
                  c3 = utfArray[i + 2];
                  string += String.fromCharCode(((c &amp; 15) &lt;&lt; 12) | ((c2 &amp; 63) &lt;&lt; 6) | (c3 &amp; 63));
                  i += 3;
              }
  
          }
          return string;
      }
  }
  </code></pre>
  
  <p>to be used like this</p>
  
  <pre><code>&lt;script&gt;
      var socket = io.connect('http://localhost:8091');
      socket.on('Events_AreaIntervento_AreaInterventoCreata:Events', function (msg) {
          var jsonString = Utf8.decodeArray(msg.data);
          alert(jsonString);
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><em><strong>But I still wonder if it is better to do it on the client, or on the node.js server, and how to do it on the node.js server ?</em></strong></p>