Giter Site home page Giter Site logo

map_updater's Introduction

map_updater

A tiny socket using SocketIo to update a general map when something changes in the application

Dependencies

You must install Node (and socketio if you have any trouble using this)

Usage

For the server, after cd to this project's folder you should run:

node socketio.js

And that will start the mini server

In your application you must have the core.js (utilities/core.js) file and it must be available when you want to call any functions

For example you can make a file like this that manages the Updates

//javascript

var Updater = ( function () {
  var socket  = io('http://localhost:8085') //check the port if you changed it in the configuration
    , timeout = 1500

    , listenToUpdates = function () {
        socket.on('update interventions', function (data) {
          var message = 'New updates'
          showMessage(message)
          setTimeout(refreshPage, timeout)
        });
    }
    , emitEvent = function (event, incomingMessage) {
        if (event == 'new update') socket.emit(event, { message: incomingMessage })
    }
    , showMessage = function (message) {
        var alertDiv = document.querySelector('[rel="alerts"]')
        alertDiv.className = 'alert alert-warning'
        alertDiv.innerHTML = message
    }
    , refreshPage = function () {
        window.location.reload()
    }
  return {
      update    : listenToUpdates
    , emitEvent : emitEvent
  }
} () )

And then call them from your views:

// When you want to let the server knows that something happened, I usually do this after an ajax request.
InterventionUpdater.emitEvent('new update')
<!-- This goes where you want to catch the advices from the server -->
  <script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
      Updater.update();
    });
  </script>

PD: this is super beta and made for a specific project, probably you should edit the socketio.js file in order to make this work correctly

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.