Code Monkey home page Code Monkey logo

ajax_bus's Introduction

ajax_bus

Example project to study message bus from server to angular client.

The Problem

We have a server with database connection, an API surface for communicate with the server, a service layer on the client side and a controller layer for the frontend. The user make multiple actions those has to go through up and down on the full system and that is a totally async channel.

So, how should I handle the messages ?

What kind of protocoll should simplify my life ?

The setup

  • The server is indifferent.
  • The client is angularjs.
  • The communication made with JSON.

The protocoll

The messages has got the same format at every level.

var succeeded = { status: 'success',
                  data: { data1: 1, data2: 2, ... },
                  message: null | 'message' };

var error =     { status: 'error',
                  data: null,
                  message: 'detailed error message' };

The answer

Each layer should work as a proxy layer. The result from the sublayer should be proxied or replaced at case of error. The messages about the error should be defined on the enduser perspective.

app.service('service', function($http, $q, config) {
 this.get = function(url) {
   var deferred = $q.defer();
   
   $http.get(config.base + url).then(
     function(resp){
       deferred.resolve(resp.data);
       },
     function(resp){
       deferred.resolve({status: 'error', data: null, message: resp.statusText});
       // In this case we needn't another function for the reject
     }
   );
   
   return deferred.promise;
 };

The same on the server side

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
    
try {
  
  if(!is_int($request)) {
    throw new Exception('It isn\'t a number. PHP');
  }
  echo json_encode(['status' => 'success',
                    'data' => $request + 1,
                    'message' => ''
                   ]);
} catch (Exception $e) {
  echo json_encode(['status' => 'error',
                    'data' => null,
                    'message' => $e->getMessage()
                    ]);
}

The controller receives only 'success' or 'error' returns. No need to handle different cases on the controller level. If the return is 'success', then the controller allowed to use it, but if it is 'error', then it has the error message for the user. The server side PHP also can send error messages to the frontend.

ajax_bus's People

Contributors

jsheperd avatar

Watchers

James Cloos avatar  avatar

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.