O'Reilly logo

Mastering Reactive JavaScript by Erich de Souza Oliveira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Service implementation

To implement our command service, change your services.js file, switching the implementation of the command service to use this:

command(payload){         switch(payload.action){                 case 'roll_a_dice':                         this.sendMessage({                                from:'server',                                to:payload.from,                                content:'Result is                                '+Math.round(1+Math.random()*5)                        });                         break;                  case 'echo':                          this.sendMessage({                                 from:'server',                                 to:payload.from,                                 content:payload.content                         });                          break;                 default:                 this.sendMessage({                        from:'server',                        to:payload.from,                        content:'The action '+payload.action +' is not                        supported'});                  }     } 

The implementation of our service is simple: we just use a switch block on the action property to decide which message we must send. The first case is roll_a_dice, in this case we send ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required