12

Having watched Meteor Framework screencast. I noticed that changing the database seamlessly changes the data in browser. Usually AJAX, just reloads a part of page every few seconds but here I didn't noticed browser reloading. How did they achieve that in Meteor? Is it Node.js dependent?

UPDATE: Toby Catlin poses another interesting question. How does Meteor handle different browsers?

Dan Dascalescu
  • 110,650
  • 40
  • 276
  • 363
Daniel Fath
  • 11,537
  • 6
  • 39
  • 72

2 Answers2

17

They use both Session and Meteor.autosubscribe (from Meteor API) to ensure that changes are reflected on the clients.

These Meteor APIs use XHR (XMLHttpRequest) by SockJS. SockJS is WebSocket emulation utility. So when something changes on the server, SockJS ensures that an XHR is sent, and the changed data is in the JSON response.

Yes, Meteor is fully dependent on Node.js. From the Meteor docs:

A Meteor application is a mix of JavaScript that runs inside a client web browser, JavaScript that runs on the Meteor server inside a Node.js container, and all the supporting HTML fragments, CSS rules, and static assets. Meteor automates the packaging and transmission of these different components. And, it is quite flexible about how you choose to structure those components in your file tree.

The only server asset is JavaScript. Meteor gathers all your JavaScript files, excluding anything under the client and public subdirectories, and loads them into a Node.js server instance inside a fiber. In Meteor, your server code runs in a single thread per request, not in the asynchronous callback style typical of Node. We find the linear execution model a better fit for the typical server code in a Meteor application.

Sources: http://docs.meteor.com/ and https://github.com/meteor/meteor

Community
  • 1
  • 1
Hudon
  • 1,627
  • 17
  • 28
5

There are a few techniques that allow the server to push data into the browser without the browser needing to request it. The term for such technology is Comet [wikipedia.org] and most techniques are related to AJAX (there was a bleach called Comet and a cleaning product called Ajax). There are a number of connection types: long polling, streaming XHR, forever frame, server-send-events and websockets. Socket.IO is a nice library that provides connection types to streaming servers.

You do need a server that will support Comet connections. You can google for current ones but off the top of my head: node.js, tornado, cometd, orbited, Jetty streaming

I would guess that Metor would use different connection types depending of the capabilities of the browser, eg websocket for Chrome and long polling for IE. If anyone can give a more specific answer i would be interested

Daniel Fath
  • 11,537
  • 6
  • 39
  • 72
  • Using WebSockets you dont need to have browser requesting data, because of constant connection established between browser and server. Server is able to send data to client as far as they are connected. – moka Apr 11 '12 at 14:56
  • If it's using socket IO I believe it will fall back to long polling for old browsers. For new ones it would open a webSocket – climboid Apr 29 '12 at 20:56
  • Comet is now known by W3C as Server Sent Events (SSE). See: http://stackoverflow.com/questions/1964494/how-to-make-all-connected-browsers-reload-initiated-by-a-server-side-event – Ciro Santilli新疆棉花TRUMP BAN BAD Dec 10 '14 at 16:22