JavaScript Sending a list of custom objects from server to client

I made a Character class shared by the client and server in the same js file.

The server instanciates these characters and stocks them in a characterList object. I send it to the client with socket.emit( 'updated_characters', characterList ), but the client gets back an object of Object, instead of an object of Character, so I can't use my Character's methods on them.

How can I get around that ?

Answer:1

You can't send custom object types directly using socket.io. socket.io uses JSON as the transport format for objects and JSON does not have the ability to record or rebuild custom object types.

So, what you need to do is to create a serialized format for your objects (which will also likely be JSON) and then when you read the data on the other end, you will construct a custom object in your own code and then pass it this data in the constructor from which it will initialize itself.

The canonical ways to do this is to create a serialize method for your custom object that creates a new plain object that contains all the relevant info that needs to be sent over the wire and does not contain references to other objects (since that can't be serialized). Keep in mind that objects often have housekeeping information that does not necessarily need to be sent to the other end. Then, create an initialize method for your custom object that can take this serialized data and properly initialize your object that you can use on the other end.

Here's a simple example:

function MyObject(data) {
   if (data) {
      this.init(data);
   } else {
       this.count = 0;
       this.greeting = "hello"
   }
}

MyObject.prototype = {
    init: function(data) {
       this.greeting = data.greeting;
       this.count = data.count;
    },
    getData: function() {
        return {greeting: this.greeting, count: this.count};
    },
    talk: function() {
        say(this.greeting);
    }
};

Then, on the sending end of things, if you have an instance of your object in item, you would send the data with:

socket.emit('updated_characters', item.getData());

And, on the receiving side of things, you would have this:

socket.on('updated_characters', function(data) {
    var item = new MyObject(data);
    // do something with the item here
});
Answer:2

I have two divs <div class="tab-pane active" id="home"> <div class="tab-pane" id="profile"> Each of this div has forms in it which has a hidden field <input type="hidden" name="...

I have two divs <div class="tab-pane active" id="home"> <div class="tab-pane" id="profile"> Each of this div has forms in it which has a hidden field <input type="hidden" name="...

  1. javascript class value change
  2. javascript classlist value
  3. javascript classname value
  4. javascript class value get
  5. javascript class value set
  6. javascript value to classic asp
  7. javascript value as class name
  8. javascript read class value
  9. javascript get class value of element
  10. javascript check class value
  11. javascript add class value
  12. javascript update class value
  13. javascript input class value
  14. javascript span class value
  15. javascript remove class value
  16. javascript if class value
  17. javascript html class value
  18. javascript div class value
  19. javascript td class value
  20. javascript pass class value

Currently I am using React on Rails application (using react-rails library) and I wish to use some third party React components. How to use third party component without adding dependency to ...

Currently I am using React on Rails application (using react-rails library) and I wish to use some third party React components. How to use third party component without adding dependency to ...

  1. react js without npm
  2. install react without npm
  3. use react without npm
  4. create react app without npm
  5. run react app without npm start

Update: A Solution below! I'm fairly new to website development but I've been tasked with developing e2e (end-to-end) tests for a developing website that uses AngularJS. As a result I've been looking ...

Update: A Solution below! I'm fairly new to website development but I've been tasked with developing e2e (end-to-end) tests for a developing website that uses AngularJS. As a result I've been looking ...

Apologies in advanced, but I'm a complete noob at Polymer. So far I've done an HTML file (treated as external dialog box), with some stuff in it. First off, the user clicks a button from the main ...

Apologies in advanced, but I'm a complete noob at Polymer. So far I've done an HTML file (treated as external dialog box), with some stuff in it. First off, the user clicks a button from the main ...

  1. create click event javascript
  2. create click event js
  3. create click event in jquery
  4. create click event
  5. create click event c#
  6. create click event angular
  7. create click event vb.net
  8. create click event on div
  9. create click event in html
  10. create click event object
  11. create click event in java
  12. create click event programmatically
  13. create click event using javascript
  14. create click event using jquery
  15. create mouse click event javascript
  16. create button click event c#
  17. react create click event
  18. create button click event in android
  19. javascript create click event object
  20. jquery create click event dynamically