Giter Site home page Giter Site logo

angular-socket's Introduction

angular-socket

A wrapper for websockets that exposes them as angular.js services

Usage Example

I may change this API, as I'm not sure how much I like it - it's not listening to events, it's providing callbacks. But, here's how you would use this.

var app = angular.module("myApp", ["myk.sockets"])
	.directive("myDirective", ["Socket", function(Socket) {
		return {
			restrict: "E",
			replace: "true",
			template: "<div><div class='alert' ng-repeat='message in messages'>{{message}}</div></div>",
			link: function(scope, element) {
				var messages = ["First message"];
				var socket = Socket.register("ws://myserver.com/mysocketendpoint", {
					onopen: function(e) {
						console.log("Connected!");
					},
					onmessage: function(e) {
						messages.push(e.data);
						scope.$apply();
					},
					onclose: function(e) {	
						console.log("Disconnected!");
					}
				});

				// call these from a controller or whatever you want.
				scope.send = function(payload) {
					socket.send(payload);
				}

				// careful with this one, since it nukes the whole connection for everyone currently using it.
				scope.close = function() {
					socket.close();
				}
			}
		}
	}]);

Note that the nice thing about this approach is that your event handlers are cummulative - you can reuse this directive in multiple places, register to the same URL multiple times. It will only ever make one connection per URL, but it will call every handler you provide. So if you have three different directives spread across a large app you can easily have them all listen to input from the same URL without having any coupling in your app, if you're into that sort of thing.

angular-socket's People

Contributors

mbilokonsky avatar

Watchers

 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.