socket.io Lab

Socket IO Lab - Emoji Racer (10 points for In-Class Project/Quiz Grade)

Submission Process

  • work in groups of 2 or 3
  • submit using this form
  • each person on the team should submit their own individual form

Scoring

  • +7 points for showing up and submitting form
  • +2 points form submitted with a reasonable amount of valid looking code
  • +1 point code deployed on glitch.com (kind of optional, since you basically get 90% for just submitting a form with some code!)

Overview

Goals / Topics Covered

You'll be using the following concepts:

  • socket.io
  • some simple dom manipulation
  • absolute or fixed positioning

Description

Make a real time web app that:

  1. displays two emoji
  2. displays two buttons
  3. displays a finish line
  4. clicking on one button moves one emoji
  5. everyone connected to the game can click either button
  6. everyone connected to the game can see the emoji move in real time
  7. when someone new connects to the game, the should see the current position of both emoji
  8. (optional) end game and show message when one emoji crosses the finish line

example emoji racer animation

Instructions

Setup

Use the one-page version of the slides to guide you through socket.io:

https://foureyes.github.io/csci-ua.0480-spring2018-008/slides/23/socketio.html?print-pdf#/

  1. create a directory to store your project
  2. create your package.json and install these packages:
    
    npm init
    npm install --save express socket.io
    
  3. use this boilerplate code for the server (perhaps in server.js or app.js):
    
    const express = require('express');
    const app = express();
    const server = require('http').Server(app);
    const io = require('socket.io')(server);
    app.use(express.static('public'));
    // server code goes here!
    // first listen for connection using io.on
    // then... within callback, use socket.on, socket.emit, socket.broadcast, etc.
    // NOTE THAT WE ARE LISTENING WITH server, NOT app!
    server.listen(3000);
    
  4. use this boilerplate code for the markup (in public/index.html):
    
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <script src="/socket.io/socket.io.js"></script>
    <script src="racer.js"></script>
    <button class="player1Btn">Move Tears of Joy &rarr;</button>
    <div class="play-area">
      <div class="racer player1">&#128514;</div>
      <div class="racer player2">&#128561;</div>
    </div>
    <button class="player2Btn">Move Face Screaming &rarr;</button>
    </body>
    </html>
    
  5. use this boilerplate code for the client (in public/racer.js):
    
     const socket = io();
    

Deployment

Deploying to glitch.com

  1. go to glitch.com/edit
  2. modify the existing package.json so that it has both socket.io and express as requirements (but keep everything else the same)
  3. add/modify necessary files!
    • for example…
    • modify server.js (make sure you're listening with server obj)
    • add public/racer.html
    • change the name of public/client.js to public/racer.js
    • etc.
  4. change the port so that it looks in the env for the port number!
    • server.listen(process.env.PORT);
  5. click on the look link…
    • instantly deployed app!
    • (click on logs link to see server output)

Major Hints (Click to Reveal)