Author: saqibkhan

  • Using Accounts and Authentication

    Meteor has built-in support for user authentication. You can use packages like accounts-base, accounts-password, and more.

    Setting Up Accounts:

    meteor add accounts-base accounts-password
    

    Create a New User:

    Meteor.methods({
      'createUser': function (username, password) {
    
    Accounts.createUser({
      username: username,
      password: password
    });
    } });

    Log In a User:

    Meteor.loginWithPassword('username', 'password', (error) => {
      if (error) {
    
    console.error('Login failed:', error);
    } else {
    console.log('Login successful');
    } });
  • Security Best Practices

    **1. Data Validation: Always validate data on the server side to prevent malicious data from being saved.

    **2. Use Meteor’s check Package: For validation:

    import { check } from 'meteor/check';
    
    Meteor.methods({
      'createUser': function (user) {
    
    check(user, {
      username: String,
      email: String
    });
    // Proceed with user creation
    } });

    **3. Limit Publications: Only publish the data needed by the client to avoid exposing sensitive data.

    Meteor.publish('userData', function () {
      return Meteor.users.find({}, { fields: { profile: 1, emails: 1 } });
    });
    
  • Advanced Meteor Features

    Reactive Programming

    Meteor leverages reactive programming to automatically update the UI when the underlying data changes. This is a powerful feature and is built into Meteor’s data layer.

    Reactive Variables:

    You can create reactive variables using ReactiveVar:

    import { ReactiveVar } from 'meteor/reactive-var';
    
    const count = new ReactiveVar(0);
    
    // To get the value
    count.get(); 
    
    // To set the value
    count.set(1);
    

    Tracker:

    Tracker allows you to automatically re-run code when reactive data sources change.

    import { Tracker } from 'meteor/tracker';
    
    Tracker.autorun(() => {
      console.log('This will run whenever the reactive variable changes');
    });
    

    Method Calls

    Meteor methods are used to define functions that can be called from the client to the server. This is useful for executing server-side logic from the client.

    Define a Method:

    // On the server
    Meteor.methods({
      'myMethod': function (param) {
    
    check(param, String);
    return Received: ${param};
    } });

    Call a Method:

    // On the client
    Meteor.call('myMethod', 'Hello', (error, result) => {
      if (error) {
    
    console.error('Error:', error);
    } else {
    console.log('Result:', result);
    } });

    Server-Side Code

    You can use the server side for tasks like user authentication, data validation, and more.

    Example of a Server-Side Method with Validation:

    Meteor.methods({
      'insertItem': function (item) {
    
    check(item, {
      name: String,
      quantity: Number
    });
    // Insert item into the collection
    Items.insert(item);
    } });
  • Deploying Your App

    You can deploy your Meteor app to Galaxy, a Meteor hosting service, or to other platforms like Heroku. For Galaxy:

    1. Login to Galaxy:bashCopy codemeteor login
    2. Deploy to Galaxy:
    meteor deploy yourapp.meteorapp.com --settings settings.json

    For other platforms, you might need to follow their specific deployment instructions, which typically involve configuring environment variables and building your app.

  • Routing

    Meteor doesn’t come with a built-in routing system, but you can use third-party packages like iron:router or kadira:flow-router. Here’s an example using iron:router:

    meteor add iron:router
    

    Define Routes:

    import { Router } from 'iron:router';
    
    Router.route('/', function () {
      this.render('home');
    });
    
    Router.route('/about', function () {
      this.render('about');
    });
    
  • Adding Packages

    Meteor has a rich ecosystem of packages. To add a package, use:

    meteor add package-name
    

    For example, to add the popular accounts-base package:

    meteor add accounts-base
    
  • Basic Concepts

    **1. Blaze Templating: Meteor comes with its own templating system called Blaze. Here’s an example of a simple Blaze template:

    <head>
      <title>My First Meteor App</title>
    </head>
    
    <body>
      <h1>{{header}}</h1>
      {{> myForm}}
    </body>
    
    <template name="myForm">
      <form class="my-form">
    
    &lt;input type="text" name="textField" placeholder="Type something">
    &lt;button type="submit">Submit&lt;/button>
    </form> </template>

    **2. Collections: Collections are a way to manage and store data. Here’s how to define a new collection:

    // In a file within the imports/ directory
    
    import { Mongo } from 'meteor/mongo';
    
    export const Items = new Mongo.Collection('items');
    

    **3. Publications and Subscriptions: To manage data flow between the server and client, you use publications and subscriptions.

    Server Side:

    Meteor.publish('items', function () {
      return Items.find();
    });
    

    Client Side:

    Meteor.subscribe('items');
    
  • Project Structure

    After creating a new Meteor app, you’ll see a few default directories and files:

    • client/: Code and files that are specific to the client side.
    • server/: Code that runs on the server side.
    • imports/: Code that can be imported by both client and server.
    • public/: Static assets that are served directly.
    • package.json: Lists your app’s dependencies.
  • Setting Up Meteor

    Install Meteor:

    1. For macOS/Linux: Open your terminal and run:
    curl https://install.meteor.com/ | sh
    1. For Windows: Download and run the installer from the Meteor website.

    Create a New Meteor Project:

    meteor create my-new-app
    cd my-new-app
    meteor
    

    This will create a new directory with a basic Meteor app and start a local server.

  • Introduction to Meteor.js

    Meteor is a framework that uses JavaScript both on the client and server sides. It includes a built-in system for managing real-time updates, making it great for applications where live data is crucial.