EJSON e DDP

Image title

In the previous article (But what’s Meteor?) I made a overview about Meteor, now let’s see a little more of each item of an application Meteor architecture, starting with EJSON and DDP.

EJSON

EJSON is a JSON extension that supports most types eg Date (Javascript Date), Binary (Javascript Uint8Array or EJSON.newBinary) and user-defined types (EJSOPN.addType), an example is the Mongo.ObjectId. JSON by itself, does not provide this kind of support.

Some examples:

[code language=”javascript”]

EJSON.stringify ({When: new Date})

”{“ When “: {“ $ date “: 1412884295442}}”

EJSON.stringify ({imageData: new Uint8Array (10)})

”{“ ImageData “: {“ $ binary “,” aaaaaaaaaaaaaa == ‘}} “

EJSON.stringify ({_ id: new Mongo.ObjectID})

”{“ _id “: {“ $ Type “,” oid “,” $ value “:” 34c420c028fb169c31041ab0 “}}”

[/code]

Another important feature of EJSON is your stringify method, when used with the option {canonical: true}, unlike JSON, ensures that the output values are always the same for a given input value.

DDP

DDP (Distributed Data Protocol) is the Meteor’s backbone. It’ss a communication protocol based on JSON (EJSON) used for communication between client and server. The current implementation of Meteor is based on WebSockets and SockJS. SockJs is an emulator of WebSocket that Meteor uses when Websockets is not available, in other words, when using older browsers.

What can I do with it?

You can execute remote methods invocations against the RPC server (Remote Procedure Call).

You can subscribe to ‘data sets’ to stay informed in real time about any change that happens within these sets. What do you mean?

For example, when you, client, accesses this blog, a subscription happens:

Meteor.subscribe (‘posts’, this.language (), this.findOptions ());

You subscribes (sub) under “posts”.

RCuG2H4YzzHVqIDbY27Y5p_aY0hYaESyt6fkR4cQHAReZQ6Lj9WUpe15w-xfMVbcoaz2e-euSKmI5eCZTuYLbbABeNa_sfUR4Bp4tPjRJ_yv81HEWeeub7Rk1NyEm5lstZd7jHw

Server responds with the data found (added).

ccikUvnxWHDtrgw-glC8ngHL5OjmFsX4UfYtDREEOMLet-GErBnSWwyke7aXweK4m5XRPzuaoSkr46wjIsLiAxDwyUXmB1zPXQaJhl8zcHlSbTkKbTt3P6MJmzX6yfHsHBd-MB0

Server tells the client that has sent all (ready).

_F-E0xfC-dXM_qdsOfFfntv1Nws5ueFMJQBM6g-cYy53Q_J0MNxbadCG7tT5dOVnvfZWMxLWH_QJZJJohOmcakaeaVIovJIhS9jXu2Fi7RZYkKod-6I36etD2g_cm4Dfb0DmEuI

After that, if any changes happen within that set of data, such as a new post, “you”, you will be notified.

Another cool thing is that client and server do not know who with they are talking, they just know they are speaking DDP, in other words, clients and servers do not necessarily need to be Meteor applications. An example would be a native mobile application talking to a Meteor server using DDP.

An example is the meteor-ios, written to integrate iOS native applications with Meteor servers using DDP. Here’s a snippet of code into Swift:

[code language=”javascript”]

let Meteor = METCoreDataDDPClient (serverURL: NSURL (string, “wss: //servidor-meteor.com/websocket”))

UIApplicationMain

class AppDelegate: {UIApplicationDelegate

func application (application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

Meteor.connect ()

}

}

[/code]

When a Meteor client is loaded, a new connection to the server is established.

Meteor.connection = DDP.Connect (DDP_DEFAULT_CONNECTION_URL);

And it is through the object returned by DDP.Connect that the client communicates with the server, two of the most used methods of this object are call and subscribe; apply, status, reconnect, disconnect and methods are set directly on the Meteor namespace, that’s why whe can subscribe using Meteor.subscribe and not Meteor.connection.subscribe.

Observing messages exchange.

A very useful tool to see this happening is the DDP Analyzer. Installation is very simple.

[code language=”shell”]

npm install -g ddp-analyzer

[/code]

After installation it’s just start the proxy

[code language=”shell”]

ddp-analyzer-proxy

[/code]

And export the environment variable DDP_DEFAULT_CONNECTION_URL

[code language=”shell”]

export DDP_DEFAULT_CONNECTION_URL = http: // localhost: 3030

[/code]

What you are doing here is telling to Meteor client, to connect to another server, in this case

localhost: 3030

Another variable that was not mentioned in the DDP Analyzer documentation but, that helps  viewing of DDP messages exchange is DISABLE_WEBSOCKETS. Export this variable to true (optional)

[code language=”shell”]

export DISABLE_WEBSOCKETS = true

[/code]

Okay, now it’s just start you meteor application to view the messages exchange client and server.

 

Sources

http://meteorpedia.com/read/DDP_Clients

https://www.meteor.com/ejson

http://docs.meteor.com/#/full/ejson

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

https://meteorhacks.com/introduction-to-ddp.html

https://meteorhacks.com/discover-meteor-ddp-in-realtime.html

https://github.com/arunoda/meteor-ddp-analyzer

https://github.com/meteor/meteor/blob/devel/packages/ddp/DDP.md

https://www.meteor.com/ddp

Allan de Queiroz

Allan de Queiroz
London based software engineer

XServer forward from Linux text mode for Headless purposes.

Hello, this post is about XServer forward from Linux text mode, **not ssh forward, anything related to VNC** or things like that.Recently...… Continue reading