AngularJS Socket.IO Integration

0.7.0 · abandoned · verified Wed Apr 22

The `angular-socket-io` package provides a Bower component designed to integrate Socket.IO with AngularJS applications. As of version 0.7.0, it is specifically built for AngularJS (version 1.x) and relies on the Bower package manager for installation, making it largely incompatible with modern JavaScript ecosystems using npm or Yarn, and contemporary Angular (2+). Its primary function is to wrap the Socket.IO client, ensuring that events are properly integrated into AngularJS's digest cycle. This allows developers to use Socket.IO's `on`, `emit`, `removeListener`, and `removeAllListeners` methods, alongside a unique `socket.forward` API that broadcasts Socket.IO events to AngularJS's `$scope` or `$rootScope` event system. The project appears to be unmaintained, with its last significant update aligning with the AngularJS era, thus lacking ongoing release cadence and support for current web development practices.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure `angular-socket-io` as an AngularJS factory, instantiate a `socketFactory` instance, forward Socket.IO events to AngularJS scopes, and use the socket instance in a controller to send and receive messages.

// First, ensure AngularJS and the Socket.IO client library are loaded in your HTML:
// <script src="path/to/angular.min.js"></script>
// <script src="/socket.io/socket.io.js"></script>
// <script src="path/to/bower_components/angular-socket-io/socket.js"></script>

// Define your main AngularJS module and add 'btford.socket-io' as a dependency
angular.module('myApp', [
  'btford.socket-io',
  'myApp.MyCtrl' // Assuming you have a controller named MyCtrl
])
.factory('mySocket', function (socketFactory) {
  // Instantiate a socket. This can be configured with 'ioSocket' for custom connections.
  var mySocket = socketFactory();

  // Optionally, forward socket events to AngularJS's event system.
  // This makes 'error' events available via $scope.$on('socket:error', ...)
  mySocket.forward('error');
  // Forward multiple events, here 'connect' and 'disconnect' to $rootScope by default (null scope)
  mySocket.forward(['connect', 'disconnect'], null);

  return mySocket;
})
.controller('MyCtrl', function ($scope, mySocket) {
  // Use the injected socket instance
  $scope.messages = [];
  $scope.inputMessage = '';

  // Listen for a custom 'message' event from the server
  mySocket.on('message', function (data) {
    $scope.messages.push(data);
  });

  // Function to send a message to the server
  $scope.sendMessage = function () {
    if ($scope.inputMessage) {
      mySocket.emit('sendMessage', { text: $scope.inputMessage, timestamp: new Date() });
      $scope.inputMessage = ''; // Clear input after sending
    }
  };

  // Listen for forwarded socket error events within this specific scope
  $scope.$on('socket:error', function (ev, data) {
    console.error('Socket Error:', data);
    $scope.messages.push({ text: `Error: ${data.message || 'Unknown error'}`, type: 'error' });
  });

  // Example of emitting an event when the controller initializes
  mySocket.emit('joinRoom', { room: 'general' });
});

view raw JSON →