Git Product home page Git Product logo

angularstompdk's Introduction

AngularStomp

Angular service to Web-Stomp

Usage

This class relies on stomp.js that can be found at: https://github.com/jmesnil/stomp-websocket/

Add the dependency to your Angular application :

    angular.module('myApp', [
        'AngularStompDK'
    ])

Configure the ngStomp to connect to your web-socket system :

    angular.module('myApp')
        .config(function(ngstompProvider){
            ngstompProvider
                .url('/ws')
                .credential('login', 'password')
                .class(SockJS);
        });

Use it inside your controller (or everywhere you want !)

    angular.controller('myController', function($scope, ngstomp) {
    
        var webSocketEndPoint = '/topic/item',
            items = [];

        ngstomp
            .subscribe(webSocketEndPoint, whatToDoWhenMessageComming);

        function whatToDoWhenMessageComming(message) {
            items.push(JSON.parse(message.body));
        }
     });

You can chain multiple subscribe

 angular.controller('myController', function($scope, ngstomp) {
    
    var items = [];
    
    ngstomp
        .subscribe('/topic/item', whatToDoWhenMessageComming)
        .subscribe('/queue/message', whatToDoWhenMessageComming);
            
    function whatToDoWhenMessageComming(message) {
        items.push(JSON.parse(message.body));
    }
 });

Don't forget to unsubscribe your callback when the $scope is detroy (or the call will append even after the scope has been detroyed)

 angular.controller('myController', function($scope, ngstomp) {
    
    var items = [];
    
    ngstomp
        .subscribe('/topic/item', whatToDoWhenMessageComming)
        .subscribe('/queue/message', whatToDoWhenMessageComming);
        
    $scope.$on('$destroy', function() {
        ngstomp
            .unsubscribe('/topic/item', whatToDoWhenUnsubscribe);
    });
    
    function whatToDoWhenMessageComming(message) {
        items.push(JSON.parse(message.body));
    }
    
    function whatToDoWhenUnsubscribe() {
        console.log("Unsubscribed !! :D);
    }
 });

Or you can give the $scope to the subscribe method to enable the auto-unsubscribe

 angular.controller('myController', function($scope, ngstomp) {

    var items = [];

    ngstomp
        .subscribe('/topic/item', whatToDoWhenMessageComming, $scope)
        .subscribe('/queue/message', whatToDoWhenMessageComming, $scope);

    function whatToDoWhenMessageComming(message) {
        items.push(JSON.parse(message.body));
    }
 });

And now (since v0.1.0) you can send back to the Web-Socket information :

 angular.controller('myController', function($scope, ngstomp) {

    var items = [];

    ngstomp
        .subscribe('/topic/item', whatToDoWhenMessageComming, $scope);

    function whatToDoWhenMessageComming(message) {
        items.push(JSON.parse(message.body));
    }

    var objectToSend = { message : 'Hello Web-Socket'},
        stompHeaders = {headers1 : 'xx', headers2 : 'yy'};
         
    $scope.sendDataToWS = function(message) {
        ngstomp
            .send('/topic/item/message', objectToSend, stompHeaders);
    }
 });

angularstompdk's People

Contributors

davinkevin avatar gtarcea avatar

Watchers

 avatar  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.