btford / btford/angular-socket-io-im
Integrating with realtime-rails
- Dominant language
- JavaScript
- Stars
- 390
- Forks
- 169
- PR merge metrics
- No merged PRs in 30d
Description
Hello Bran,
I am trying to integrate your solution here to the Rails integrated realtime-rails solution from Mike Atlas
http://mikeatlas.github.io/realtime-rails/
the challenge i am running into is that the socket interface is already being created outside of the angular code:
>
```
window.realtime = {};
window.realtime.messageQueue = [];
window.realtime.enabled = false;
```
if (typeof io != 'undefined' && io != null) {
window.realtime.token = 'e9a34830136837934f11fecddc04066f';
window.realtime.userId = '42';
window.realtime.socketIo = io.connect('http://192.168.1.2:5001/?_rtUserId=42&_rtToken=e9a34830136837934f11fecddc04066f');
}
if (window.realtime.socketIo) {
window.realtime.enabled = true;
window.realtime.socketIo.on('connect', function() {
// Give a nice round-trip ACK to our realtime server that we connected.
window.realtime.socketIo.emit('realtime_user_id_connected', { userId: window.realtime.userId });
});
// Queue up all incoming realtime messages.
window.realtime.socketIo.on('realtime_msg', function(message) {
window.realtime.messageQueue.push(message);
});
}
I have tried to point. the socket in your service declaration to window.realtime.socketIo (my appis called headcount)
https://github.com/dreadstar/headcount_app5/blob/master/app/assets/javascripts/angular/services.js.coffee (although this is a coffescript file i am actually forcing javascript)
> headcount.factory('socket', function ($rootScope) {
> // var socket = io.connect();
> // var socket = io.connect("http://localhost:5001");
> var socket = window.realtime.socketIo;
> return {
> on: function (eventName, callback) {
> socket.on(eventName, function () {
> var args = arguments;
> $rootScope.$apply(function () {
> callback.apply(socket, args);
> });
> });
> },
> emit: function (eventName, data, callback) {
> socket.emit(eventName, data, function () {
> var args = arguments;
> $rootScope.$apply(function () {
> if (callback) {
> callback.apply(socket, args);
> }
> });
> })
> }
> };
> });
My controller has the following additions to replicate your your example. it is written in coffescript:
https://github.com/dreadstar/headcount_app5/blob/master/app/assets/javascripts/angular/controllers/LocationIndexCtrl.js.coffee
> @headcount.controller 'LocationIndexCtrl', ['$scope', '$location', '$http','socket', ($scope, $location, $http, socket) ->
> $scope.locs[data.msg.obj.id].current_state= data.msg.obj.current_state
> $scope.locs[data.msg.obj.id].fanscnt= data.msg.obj.fanscnt
> console.log data
When i switch the EventName to 'realtime_msg' on the server, i can see that my messages are getting to the browser.
you can see the latest version of my code
https://github.com/dreadstar/headcount_app5
Any help would be appreciated.
Thanks,
Tyrone
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.