Skip to content

Instantly share code, notes, and snippets.

@ToeJamson
Created August 5, 2014 17:20
Show Gist options
  • Save ToeJamson/b985953b604292f83499 to your computer and use it in GitHub Desktop.
Save ToeJamson/b985953b604292f83499 to your computer and use it in GitHub Desktop.
encrypted AngularJS
<!doctype html>
<html>
<head>
<script src="https://cdn.pubnub.com/pubnub.min.js"></script>
<script src="https://cdn.pubnub.com/pubnub-crypto.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="//code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://pubnub.github.io/angular-js/scripts/pubnub-angular.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
</head>
<body>
// Register for presence events (optional)
$rootScope.$on(PubNub.ngPrsEv($scope.channel), function(ngEvent, payload) {
$scope.$apply(function() {
$scope.users = PubNub.ngListPresence($scope.channel);
});
});
// Pre-Populate the user list (optional)
PubNub.ngHereNow({
channel: $scope.channel
});
});
</script>
</body>
</html>
// we must call angular "bootstrap" since we're running two separate
// angular apps for normal/encrypted views
angular.bootstrap($('#unencrypted'),['PubNubAngularUnencryptedApp']);
<div class="container" ng-app="PubNubAngularApp" ng-controller="EncryptedChatCtrl">
<h4>Online Users</h4>
<ul>
<li ng-repeat="user in users">{{user}}</li>
</ul>
<h4>Chat History {{messages.length}}</h4>
<form ng-submit='publish()'>
<input type="text" ng-model='newMessage' />
<input type="submit" value="Send" />
</form>
<div class="well">
<ul>
<li ng-repeat="message in messages">{{message}}</li>
</ul>
</div>
angular.module('PubNubAngularApp', ["pubnub.angular.service"])
.controller('ChatCtrl', function($rootScope, $scope, $location, PubNub) {
// make up a user id (you probably already have this)
$scope.userId = "User " + Math.round(Math.random() * 1000);
// make up a channel name
$scope.channel = 'The Angular ENCRYPTED Channel';
// pre-populate any existing messages (just an AngularJS scope object)
$scope.messages = ['Welcome to ' + $scope.channel];
if (!$rootScope.initialized) {
// Initialize the PubNub service
PubNub.init({
subscribe_key: 'demo',
publish_key: 'demo',
cipher_key: 'changeme-noireallymeanit', /* adds a cipher key for secure message encryption (CHANGE THIS!) */
uuid:$scope.userId
});
$rootScope.initialized = true;
}
// Subscribe to the Channel
PubNub.ngSubscribe({ channel: $scope.channel });
// Create a publish() function in the scope
$scope.publish = function() {
PubNub.ngPublish({
channel: $scope.channel,
message: "[" + $scope.userId + "] " + $scope.newMessage
});
$scope.newMessage = '';
};
// Register for message events
$rootScope.$on(PubNub.ngMsgEv($scope.channel), function(ngEvent, payload) {
$scope.$apply(function() {
$scope.messages.push(payload.message);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment