Skip to content
πŸŽ“ Real-Time Web @cmda-minor-web Β· 2018-2019
Branch: master
Clone or download
Pull request Compare This branch is 94 commits ahead, 9 commits behind cmda-minor-web:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Real-Time Web @cmda-minor-web Β· 2018-2019 βš™οΈ

This is my repo for the Real-Time Web course

Real Time Web Screenshot

Learn HTML Together chat app

Table of Contents πŸ—ƒ

To Do πŸ“Œ

This is a list of things I want to do in this project:

  • Deal with real-time complexity
  • Handle real-time client-server interaction
  • Handle real-time data management
  • Handle multi-user support

Description πŸ“

For this course I made a web application that allows users to learn HTML together by using a build in text editor and chat. Users can write and discuss the HTML directly from their browser. Users are also able, by using the Github API, to create, commit and push to a Github repo. In the browser users are also able to see the number of commits, issues and stars the repo has. This will be the real time data.

Installing πŸ”

To install this application enter the following into your terminal:

git clone

cd real-time-web-1819

npm install

npm run server

Packages and Technologies

This project makes use of the following packages and technologies:

API πŸ’

I made use of the following API for this project:

The Github API has a bunch of different endpoints that you can use:

  "current_user_url": "",
  "current_user_authorizations_html_url": "{/client_id}",
  "authorizations_url": "",
  "code_search_url": "{query}{&page,per_page,sort,order}",
  "commit_search_url": "{query}{&page,per_page,sort,order}",
  "emails_url": "",
  "emojis_url": "",
  "events_url": "",
  "feeds_url": "",
  "followers_url": "",
  "following_url": "{/target}",
  "gists_url": "{/gist_id}",
  "hub_url": "",
  "issue_search_url": "{query}{&page,per_page,sort,order}",
  "issues_url": "",
  "keys_url": "",
  "notifications_url": "",
  "organization_repositories_url": "{org}/repos{?type,page,per_page,sort}",
  "organization_url": "{org}",
  "public_gists_url": "",
  "rate_limit_url": "",
  "repository_url": "{owner}/{repo}",
  "repository_search_url": "{query}{&page,per_page,sort,order}",
  "current_user_repositories_url": "{?type,page,per_page,sort}",
  "starred_url": "{/owner}{/repo}",
  "starred_gists_url": "",
  "team_url": "",
  "user_url": "{user}",
  "user_organizations_url": "",
  "user_repositories_url": "{user}/repos{?type,page,per_page,sort}",
  "user_search_url": "{query}{&page,per_page,sort,order}"


There are two ways to authenticate through the GitHub API. Requests that require authentication will return 404 Not Found, instead of 403 Forbidden, in some places. This is to prevent the accidental leakage of private repositories to unauthorized users.

Basic authentication

curl -u "username"

OAuth2 token (sent in a header)

curl -H "Authorization: token OAUTH-TOKEN"

Note: GitHub recommends sending OAuth tokens using the Authorization header. GitHub accepts sending OAuth tokens as a query parameter, but it is less secure because URLs can be logged by any system along the request path.

OAuth2 key/secret

curl ''

Using your client_id and client_secret does not authenticate as a user, it will only identify your OAuth application to increase your rate limit. Permissions are only granted to users, not applications, and you will only get back data that an unauthenticated user would see. For this reason, you should only use the OAuth2 key/secret in server-to-server scenarios.

Rate Limit

When there is basic authentication or 0Auth authentication, you can make u to 5000 requests per hour. For unauthenticated requests there is a rate limit of 60 request per hour.

Research πŸ•΅πŸ»

Here I explain the research for this project.


This is a sketch of the most important screen.

Homepage Sketch

Homepage sketch

Data Life Cycle

Version 1

Data Life Cycle

Version 2

Data Life Cycle v2

How It Works πŸ› οΈ

Here I explain the core features of this project.


One of the first core features is to make the user login using OAuth. This is important because we are going to not only 'read' the data from the GitHub API but also 'write'. When the user logs in they authorize the Learn HTML Together web app to read all data and both read and create repo data.

When the user arrives on the home page they can click the 'Sign in with GitHub' button which will send them to the GitHub OAuth environment. When the user authorises the login they get redirect back to the web app.

<a class="rtw-github-login" href=""><i class="fab fa-github"></i> Sign in with GitHub</a>

The when the redirect comes in the server starts handling it. The OAuthRedirect function starts by swamping the requestToken for a randomly generated accessToken using a axios() HTTP request.

When we have the accessToken we redirect the user to the /create-repo page and add the accessToken to the url. This way we can always use it when we need it. It may not be the most secure way but for now it's fine.

.get('/redirect', OAuthRedirect)

function OAuthRedirect(req, res) {
  const requestToken = req.query.code

    method: 'post',
    url: `${clientID}&client_secret=${clientSecret}&code=${requestToken}`,
    headers: {
      accept: 'application/json'
  }).then(response => {
    const accessToken =


  }).catch(err => console.error(err))

Create Repo

When the user is logged in, they can create a GitHub repo. When they fill in the form and post it the createRepo function starts. We use the octokit/rest.js npm package to act as a wrapper that makes interacting whit the GitHub API more easy.

We use createForAuthenticatedUser() to send the needed parameters for creating a repo:

  • name: The repo name.
  • description: The repo description.
  • auto_init: To create an initial commit with empty README.
  • has_issues: To enable issues.

We get the repo name and description form the form the user fills in. When the repo gets created the user gets redirected to the /dashboard page

.post('/dashboard', createRepo)

function createRepo(req, res) {
    name: req.body['repo-name'],
    description: req.body['repo-description'],
    auto_init: true,
    has_issues: true
  }).catch(err => console.error(err))



When the user creates a repo the also create a personal namespace which other people are able to join, in theory. When the repo gets created we emit the repo name to the server.

function saveRepoName() {
  const form = document.getElementById('rtw-create-repo'),
        socket = io()

  if (document.body.contains(form)) {
    form.addEventListener('submit', () => {
      let repoName = document.getElementsByName('repo-name')[0].value

      socket.emit('repo name', repoName)

      localStorage.setItem('repoName', repoName)

The emit gets handled by the server and saves the repo name into the nsp variable. We pass nsp as parameter into socketTest() which handles the other functionality's.

io.on('connection', socket => {
  socket.on('repo name', repoName => {
    let nsp = io.of('/' + repoName)


Polling Data

On the /dashboard page the user is able to see the number of issues, watchers, stars, and forks. We use an setInterval() to poll the function every 10 seconds to check for new data and render it.

function getRepoData() {
  const getUrl =,
        userToken = getUrl.split('access_token=')[1],
        form = document.getElementById('rtw-text-editor-form')

  let repoDataSocket = io('/repo-data'),
      repoName = localStorage.getItem('repoName')

  if (document.body.contains(form)) {
    fetch('', {
      headers: {
        Authorization: 'token ' + userToken
      .then(res => res.json())
      .then(res => {

        res.forEach(i => {

          if ( === repoName) {
            localStorage.setItem('repoData', JSON.stringify(i))

      .catch(err => console.error(err))
function repoDataInterval() {

  setInterval(repo_data.getRepoData, 10000)


Chat and Terminal

On the /dashboard page the user is able to write HTML in a in browser text editor and talk with other people about it using a chat box. Each action has its own emit.

For example when a user connects we use nsp.emit('user connected') on the server, we handle that emit client side by adding a 'Username connected' to the chat.

function runSocket(nsp) {
  nsp.on('connection', socket => {
    nsp.emit('user connected')

    socket.on('editor input', input => {
      if (input.startsWith('<') && input.endsWith('>')) {
        const getAtrb = /(<)([\w\d]*)/g,
              getAtrbVal = /(>)([\w\d\s\W]*)(<)/g

        let atrb = getAtrb.exec(input),
            atrbVal = getAtrbVal.exec(input)

        nsp.emit('create element', atrb, atrbVal)
      } else if (input.startsWith('/')) {
        const getCommand = /(\/)([\w\d]*)/g

        let command = getCommand.exec(input)

        nsp.emit('call command', command)
      } else {
        nsp.emit('editor input', input)

    socket.on('chat message', msg => {
      let splitMsg = msg.split(' ')

      nsp.emit('new message', splitMsg)

    socket.on('disconnect', () => {
      nsp.emit('user disconnected')
function socket() {
  const textEditor = document.getElementById('rtw-text-editor-form'),
        editorInput = document.getElementById('rtw-text-editor'),
        editorOutput = document.getElementById('rtw-text-editor-output'),
        chatBox = document.getElementById('rtw-chat-box-form'),
        chatInput = document.getElementById('rtw-chat-box'),
        chatOutput = document.getElementById('rtw-chat-messages'),
        chatMessages = document.getElementById('rtw-chat-messages'),
        form = document.getElementById('rtw-text-editor-form'),
        socket = io()

  let userName = localStorage.getItem('userName'),
      getRepoName = localStorage.getItem('repoName'),
      dashboard = io('/' + getRepoName)


  // ---------- TEXT EDITOR ---------- //

  textEditor.onsubmit = (e => {

    dashboard.emit('editor input', editorInput.value)

    editorInput.value = ''

    return false
  dashboard.on('create element', (atrb, atrbVal) => {
    let createElement = document.createElement(atrb[2])

    createElement.textContent = atrbVal[2]

  dashboard.on('call command', command => {
    const p = document.createElement('p')

    p.textContent = 'Lets start with something simple, type "Hello World!" in a <h1> tag.'

  dashboard.on('editor input', input => {
    const p = document.createElement('p')

    p.textContent = 'Not a valid HTML tag'
    p.className = 'rtw-not-valid'


  // ---------- CHATBOX ---------- //

  chatBox.onsubmit = (e => {

    dashboard.emit('chat message', chatInput.value)

    chatInput.value = ''

    return false
  dashboard.on('new message', splitMsg => {
    let htmlElement = []

    const htmlElementPush = => {
            if (e.startsWith('<') && e.endsWith('>')) {
              const span = document.createElement('span')

              span.textContent = e
              span.className = 'rtw-chat-html'

          removeHTML = => {
            if (e.startsWith('<') === true) {
              return ''
            } else {
              return e
          joinMsg = removeHTML.join(' ') ,
          li = document.createElement('li')

    li.textContent = userName + ': ' + joinMsg


    if (htmlElement.length > 0) {
  dashboard.on('user connected', () => {
    const li = document.createElement('li')

    li.textContent = userName + ' connected'
    li.className = 'rtw-user-connected'

  dashboard.on('user disconnected', () => {
    const li = document.createElement('li')

    li.textContent = userName + ' disconnected'
    li.className = 'rtw-user-disconnected'


Sources πŸ“š

This is a list of all the sources I used during this project:

Honourable Mentions

Licence πŸ”“

MIT Β© Bas Pieren

You can’t perform that action at this time.