Facebook Instant Games Samples

repository·main·Indexed 42 days ago

https://github.com/fbsamples/fbinstant-samples

A collection of practical examples for integrating the FBInstant SDK into games. Includes client-side implementations for ads, leaderboards, in-app purchases, and cross-promotion, as well as server-side Node.js examples for secure backend validation and bot communication.

Tokens
1.4K
Snippets
5
Records
7
Agent score
4%

What's inside Facebook Instant Games

  1. Run Client-Side examples

    main

    Client-side examples are located in the examples/ directory. You can run them using several different tasks depending on your testing needs:

    • run-mock or mock: Runs the project on localhost using a mocked version of the FBInstant SDK. This returns dummy data for every call and is ideal for rapid local iteration.
    • run-dist or dist: Runs the project on localhost using HTTPS and embeds it into the Facebook embedded player. This uses the production SDK with authentic, updated data. Note: You must have a valid FB_appId in config.json. If the game hangs at 0% loading, visit https://localhost:8000 to trust the development certificates.
    • upload: Packages and uploads the project to Facebook Web Hosting. This is used to test the game in production mode, which is necessary for testing on mobile devices.

    All tasks require the --project flag to specify which example to run.

    # Run a project with a mocked SDK
    yarn run-mock --project hello-world
    
    # Run a project with the production SDK via HTTPS
    yarn run-dist --project sending-messages
    
    # Package and upload a project to Web Hosting
    yarn upload --project ads
  2. Setup the Instant Games Examples repository

    main

    To use these examples, ensure you have Node.js and Yarn installed. Follow these steps to configure the environment:

    1. Clone or download this repository.
    2. Run yarn install in the root folder.
    3. Create a config.json file by copying or renaming config.template.json.
    4. Populate config.json with your Facebook App credentials:
      • FB_appId: Found on your app's dashboard landing page.
      • FB_uploadAccessToken: Found in the Instant Games -> Web Hosting tab of your app's dashboard.

    Ensure your Instant Game app is already set up in the Facebook developer portal before proceeding.

    {
    	"FB_appId":"<YOUR_APP_ID_HERE>",
    	"FB_uploadAccessToken": "<YOUR_UPLOAD_TOKEN_HERE>"
    }
  3. Run Server-Side examples

    main

    Server-side examples are located in the servers-examples/ directory. These are used to support client-side features like secure data validation or bot communication.

    1. Navigate to the specific server directory (e.g., servers-examples/nodejs-backend).
    2. Create a .env file by copying or renaming template.env and providing the required environment variables.
    3. Run yarn install within that directory.
    4. Start the server using node index.

    Note for nodejs-bot: This specific demo requires the backend to be hosted on an external server (like Glitch or Heroku) rather than just running on localhost to function correctly with the bot setup.

    $ cd servers-examples/nodejs-backend
    $ yarn install
    $ node index
  4. Run the Node.js backend example

    main

    The Node.js backend example is an Express-based server that integrates with a database and match management logic. It uses body-parser for JSON parsing and cors for cross-origin resource sharing. The server listens on a port defined by the PORT environment variable, defaulting to 5000.

    // The server listens on the PORT env var or 5000
    // Ensure you have required dependencies installed:
    // express, body-parser, cors, dotenv
    
    // To run with a specific port:
    // PORT=3000 node server-examples/nodejs-backend/index.js
  5. Run the Node.js bot server

    main

    The Node.js bot example uses an Express server to facilitate communication between the bot and the client. The server listens on a port defined by the PORT environment variable, defaulting to 5000 if not specified. It uses body-parser to handle JSON payloads and cors to allow cross-origin requests from the game client.

    To run the server, ensure you have a .env file configured (via dotenv) and execute the entrypoint script.

    # Ensure PORT is set if you want to use a custom port
    export PORT=5000
    node server-examples/nodejs-bot/index.js
  6. Reference: Client-Side Example Modules

    main

    The following modules are available in the examples/ folder:

    • ads: Displays Rewarded Videos and Interstitial Ads.
    • bots: Demonstrates sending/receiving data between the game client and a bot.
    • cross-promo: Prompts players to switch to another of your Instant Games.
    • hello-world: A boilerplate project for quick starts.
    • in-app-purchases: Demonstrates in-game payments.
    • leaderboards: Shows global and context-specific leaderboards.
    • secure-backend: Demonstrates backend validation of client-signed calls to prevent cheating.
    • sending-messages: Shows how to send messages to the current conversation context.
    • shortcuts: Demonstrates creating mobile home shortcuts to the game.
  7. Reference: Server-Side Example Modules

    main

    The following modules are available in the servers-examples/ folder:

    • nodejs-backend: A backend for the secure-backend client demo, used for server-side validation of client-signed calls.
    • nodejs-bot: A backend for the bots client demo, used for sending and receiving structured data from a game client.