angular-scope-sync is a service to create 3-way-bindings between angular-js, scopes and a socket-io server.
The service provides a sync function to extend the angular scope object with functionality to send and receive changes on properties.
Short example for controller
After creating all properties on $scope, call the sync function:
angular.module("app").controller("testCtrl", function ($scope , syncService ) {
$scope.title = "sync test";
syncService.sync($scope );
});
After calling the sync function the syncService emits all changes of properties to the socket.io server and listens for events from the socket.io server.
The service listens for auto generated namespaces from the server. Namespace can be built using the following schema:
syncService.sync($scope , optionalNamespace );
socketIONamespace = optionalNamespace + "/" + scopeProperty
syncService.sync($scope , optionalNamespace );
socketIONamespace = optionalNamespace + "/" + scopeProperty.objectProperty
syncService.sync($scope , optionalNamespace );
socketIONamespace = optionalNamespace + "/" + scopeArray[elementIndex]
syncService.sync($scope , optionalNamespace );
socketIONamespace = optionalNamespace + "/" + scopeArray[elementIndex].elementProperty
var port = 8080;
var path = require('path');
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var middleware = require('socketio-wildcard')();
io.use(middleware);
server.listen(port);
app.use(express.static( path.join(__dirname, '/app') ));
io.on('connection', function (socket) {
socket.on('*', function(data){
console.log("proxy : " , data.data[0] , data.data[1] );
});
});
The sync service is called with:
syncService.sync($scope , "base");
Output:
proxy : base/title sync test 1
proxy : base/object.data.data.value 123456
proxy : base/arr[2] 34
proxy : base/arr[5].data.data.value 123456
Open terminal and run
git clone https://github.com/ThomasSI/angular-scope-socket.git
cd angular-scope-socket
cd examples
npm install
cd app
bower install
cd ..
node server
Open two or more browsers with http://localhost:8080/ and play!