百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

ss-angular

> 编程语言
开源

AngularJS 对 SocketStream 的集成

99 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

AngularJS 对 SocketStream 的集成

Approach

Create an angular service that can be injected into controllers similar to the angular REST resource library.

Installing

  • Create a new socketstream app
  • Edit its package.json to add ss-angular as a dependency:
javascript
"dependencies": {
  "socketstream": "0.3.3",
  "ss-angular": "0.x.x"
}
  • run npm install
  • Reference the ss-angular request responder in your app.js:
javascript
ss.responders.add(require('ss-angular'));

This will install the backend request responder, and inject the client library for models specifically and some other wrappers around existing SocketStream features for use in AngularJS.

Add the following to your client/code/app/entry.js above the ss.server.on line

javascript
require('ssAngular');
require('/controllers');

Then define your angular controllres in client/code/app/controllers.js

You also have to make sure that you have angular.js in your client/code/lib

Running Example

If you clone this repo, there is an example. To run it, just cd to the example dir and run npm install

Model

This is the real value of this library. If you declare a model in server/model, ss-angular will poll it every pollFreq seconds and push deltas to the client. This is a read-only mechanism. In the near future, updates will be incremental, but as of version 0.4, it just pushes the entire object on each update whether it has changed or not.

It's implemented as a request responder, and it is similar to SocketStream's rpc library. It supports middleware in the same manner as rpc as well.

Server

Define a model in the server/model directory. I called this one example.js:

…

Client

This library does expose a raw ss API called linkModel and unlinkModel. You just pass it the model name and parameters and it passes you back an object every N seconds. From the ss-angular client library:

javascript
ss.linkModel(name, params, function(modelObj) {
    scope[name] = modelObj;
});

But it's much more fun if you use it via angular! I've exposed a new API in the ssAngular module, and you can use it in angular controllers like this:

javascript
angular.module('exampleApp', ['ssAngular'])
  .controller('SSCtrl',['$scope','model',function($scope,model) {
    $scope.linkModel('example',{id: 1234}, 'modelData'); 
      //this creates $scope.modelData and updates it with data in the example model filtered by id 1234
      //the modelData param is optional. If omitted, the model will exists as 'example' on the scope.
  }]);

It's that simple. You define your app's module, the ssAngular dependency and then inject model into your controller's dependencies. In this case, example is the name of the file I created in server/model and it will be the name of the model on angular's $scope. Here's the html that uses this controller:

xml

  
    

      Has Ball
      Leading By
      Server Time
    

  
  
    

      {{modelData.hasBall}}
      {{modelData.leadingBy}}
      {{modelData.serverTime}}
    

  

The data in curly brackets will be automatically updated. The link is created by simply mentioning example in the linkModel command.

Important note: you can only subscribe to a named model once in a given scope, even if you use a different where clause. The idea here is that scopes are fairly narrow, and I'd prefer to refer to the model data without the where clause for simplicity. If I allow multiple subscriptions to the same model with different where clauses in the same scope, then I have to make you refer to the model by name/where and that's ugly in the HTML. Just create a lot of scopes and keep your model context broad enough that it "fills" a scope.

The library will automatically unsubscribe from your model when the scope that created it is destroyed.

Authentication

Angular doesn't have great support for authentication. I added a few nice things that you can use to integrate with the sessions functionality in socket stream. This logic works nicely with the angular $routeProvider.

Client

Define your routes and backend authentication service module:

javascript
angular.module('exampleApp', ['ssAngular'])
  .config(['authProvider','$routeProvider','$locationProvider',function(authProvider,$routeProvider,$locationProvider) {
    authProvider.authServiceModule('example');
    authProvider.loginPath('/login');
    $routeProvider.
      when('/login', {controller:'AuthCtrl', templateUrl:'login.html'}).
      when('/app', {controller:'SSCtrl', templateUrl:'app.html'}).
      otherwise({redirectTo:'/app'});
    $locationProvider.html5Mode(true);
  }])

The login path is the path that users should be redirected to if they are not logged in.

Use the auth.authenticate and auth.logout functions:

…

This is a controller for a form that asks the user for a username and password. It passes it to the server using the auth service and it awaits the response as a $q promise.

Find out if the user is logged in using $scope.authenticated:

xml

I put this around the template used for one of my routes.

Server

In the rpc file referenced in the configuration of the auth provider, you must have this interface:

…

Pub Sub

Once you have injected this module into your app and the pubsub service into your scope, you can simply create subscriptions to scope events using angular's API, and if they are prefixed with ss-, this library will subscribe for events from scoket stream with the same name. When socketstream pushes events, they will be wrapped into Angular scope events on the appropriate scope.

This method allows you to target the events to the right part of your app and to keep in an "angular" mindset while gaining from socketstream.

See the example app. The controller looks like this:

javascript
angular.module('exampleApp', ['ssAngular'])
  .controller('SSCtrl',['$scope','pubsub',function($scope,pubsub) {
    $scope.messages = []
      $scope.$on('ss-example', function(event,msg) {
        $scope.messages.push(msg);
      });
    }]);

RPC

This is handled with the promise API in angular ($q). You just assign a rpc call to a $scope object, and the value will be assigned when the rpc returns and the GUI updated. You don't use a callback. For example:

javascript
angular.module('exampleApp', ['ssAngular'])
  .controller('SSCtrl',['$scope','pubsub','rpc',function($scope,pubsub,rpc) {
    $scope.streaming = false;
    $scope.status = "";

    $scope.toggleData = function() {
      if(!$scope.streaming) {
        $scope.streaming = true;
        $scope.status = rpc('example.on');
      }
      else {
        $scope.streaming = false;
        $scope.status = rpc('example.off', 'Too random');
      }
    };
  }]);

Here we're assigning the return value of the socketstream services we've defined to $scope.status directly. As you can see, you can pass params or not, but you can't define a callback. If you really need a callback, just call socketstream directly!

License

ss-angular is released under the MIT license.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言