• Meteor
  • Tutorial

EJSON in Methods and Subscriptions

EJSON allows us to pass date and binary types as well as custom types over the wire as regular JSON strings and supports the serialization and deserialization of those types automatically. In this screencast, we're going to look at how EJSON works hand in hand with methods and subscriptions.

Let me introduce the code. Up top I have a few functions that are just helpers and a person object with three properties. This code up here is available on the client and the server. So the person object has three properties. The first is name, which is set to a string, created at, which is set to a new date, and file, which is set to a new instance of an unsigned integer eight bit array.

And so this is a binary type. Then down below I have my client side code and my server side code. And what we want to do first is let's create a server side Meteor method that accepts a person as a parameter and look at how EJSON serializes in deserializes the types.

So let's start off by creating a Meteor method on the server. I'm going to call that method Upload Person and it's just going to take one parameter, which is a person object. And the only thing I want to do is print to the console the type of the created at property and the file property. So I'm going to call my custom type of property and pass in the person dot created at. And do the same for the file property.

And then on the client I'm just going to create a helper method called Upload Person so that we can invoke it from the browser. And this we'll just call the server method called Upload Person. And it will pass the person object from above.

OK, next I'm just going to switch over to the server and start it again so that we get a clean slate. And let's call the Upload Person method that we just created. So what we can see is that inside of our server side method, the created at property of person is of type date and the file property is of type unsigned integer eight bit array. And that's because EJSON automatically turned our person object into an EJSON string, passed it over the wire, and by the time our server side method got the person object, it had been deserialized back into its original type, including the date property and Uint8Array property.

Now, we can also use EJSON types in subscriptions. And so I've switched back over to the code view. And what I'd like to do is to create a collection and then a subscription and a publication. So to get us started, I'm going to create a collection that runs on both the client and the server called People.

And then on the client, well, we'll start on the server so that it's more clear. I'm going to create a publication. I'm going to call it People. And I'm just going to set the messages manually. So I'm going to call this dot added, which takes the collection name as the first parameter, a unique ID as the second.

So we'll call the meteor dot unique ID method. And then a set of fields that have been added to this collection. And so for that, I'm just going to pass the person object from above. And then I'm going to mark the subscription as ready by calling the ready method. And then in the client, we'll subscribe to the people publication.

OK, so let's go over to the browser now and inspect the people collection from the client. OK, over in the browser, what I'm going to do is create a variable called Person and assign it equal to people dot find one. And so that should grab the first person that we set over in the publish function. And what we notice here is that the created at property and the file property are both strongly typed. The created at property is of type date and the file properties of type Uint8Array. And then once again, we can verify this by using our type of function.

So what is happened down on line 38 on the left is that we've called the added method of the subscription saying that we want to add something, a record to the people collection, with a new unique ID. And then we pass this person object directly, which comes from up here. And the person object has two properties that have a date type, one property that has a date type and another that has a binary type.

And by the time this gets sent to the client, this person type is serialized into an EJSON format. And on the client, it's serialized back into an object with the correct types. And so that's why over on the right, we can see that the person created at is of type date and the person dot file is of type unsigned integer eight bit array or Uint8Array.

But what if we had a custom property? For example, let's create constructor function called address. And a constructor function will take a value as a parameter and it'll just set this dot value is equal to the value. And maybe that constructor or the prototype for that constructor has a method called get that just returns the value. And then in our person object, we'll create a property called Address and assign it to a new address.

Now if we come over to the browser and we take a look at the person object, by calling people dot find one and expanding the object, we see that the address of value has actually been serialized into an object. Let me get my pen. Into an object with the value property of San Francisco.

But this is just a generic object. It's not the address objects that we have over here. And so if we call the get method, we'll get an error because it's not defined. Because the type of person dot address is just an object and not an address. And so what we really want to be able to do is to tell EJSON about custom types. And we'll do that in another video.