📜 ⬆️ ⬇️

How does Meteor work from the inside?

From the translator : MeteorJS - open (MIT) full-stack JavaScript framework (both client and server). Current version 0.6.6.3 - version 1.0 is planned to be released at the beginning of 2014. Publications on Habré: 1 , 2 , 3 .

The article review the network architecture of the Meteor application.

Meteor as a server, Meteor as a client


Meteor, from the point of view of browsers, proxy servers, routers, and other network components, is, in fact, a common web application. Although, in fact, the Meteor application consists of two main parts: the part that works inside the browser and the part that works as a server . These two parts are set up in such a way as to interact with each other in a way typical of many modern web applications (such as Gmail or Trello ).
')
image
Meteor allows developers to create applications without worrying about the complexity of client-server interaction.

Meteor handles three types of requests


If you dig deeper, Meteor handles three types of requests. Here they are:


Static files

Static files are images and other similar resources from / public . Meteor processes these files automatically at startup.

Additionally, Meteor minifies and sticks together all the JavaScript (including templates that are precompiled into JavaScript) and CSS files, rendering them static.

DDP messages

DDP is a protocol that Meteor uses to interact between the client and server. All client data subscriptions, remote procedure calls and MongoDB operations are all done using the DDP protocol. At the same time - this is a fairly lightweight protocol. Messages can be viewed using a convenient tool - ddp-analyzer .

HTTP requests

Despite the fact that the official documentation does not yet have information about this, Meteor can handle HTTP requests, like other normal applications. For example, file downloads are handled by Meteor as HTTP requests. Read the question on StackOverflow for details.

Meteor has two servers inside


Although Meteora listens on only one port, inside it works like two separate servers:
image
HTTP server

HTTP server is used to transfer static files and process HTTP requests. Meteor uses the connect Node.js module for this purpose.

DDP server

The DDP server handles all data publishing, MongoDB operations and Meteor methods. Meteor uses SockJS , as a transport. Essentially, the DDP is a SockJS server, a modified Meteor.

MongoDB and Meteor


You can scale the HTTP and DDP Meteor servers by running multiple Meteor instances connected to the same MongoDB database, but the result will not be perfect. This is due to the way Meteor polls MongoDB for changes — if one instance of Meteor updated the data in MongoDB, it may take a few seconds before other instances see this update and distribute it to connected users.

To illustrate this, imagine that two Meteor instances (A and B, with the corresponding HTTP and DDP servers in each) are served by the same chat. In front of them is a proxy server, randomly connecting users to one of these instances. If someone connected to instance A writes a chat message, users connected to instance B will not see it in real time, they will have to wait a few seconds until server B is aware of the change and distributes it to their browsers.

In the following articles, I will show how to configure Meteor and MongoDB to get rid of this effect.
Such polling logic is very expensive , so that it can be used on a production server, the best solution would be to use MongoDB Oplog. In Meteor 1.0, it will be so, in the meantime, you can use Smart Collections .


From the translator: if it is interesting to someone, in my opinion the best resources on Meteor at the moment are:
  1. Evented Mind - educational screencasts;
  2. MeteorHacks - articles and news about Meteor;


P.S. A new hub has appeared on the Habr : Meteor.JS - subscribe.

Source: https://habr.com/ru/post/205878/


All Articles