triniwiz / nativescript-socketio

Socket.IO for nativescript
Apache License 2.0
71 stars 31 forks source link

Subject can´t emit changes in callback #13

Closed xLama closed 8 years ago

xLama commented 8 years ago

I am not sure if is an nativescript-sockeio error or a Nativescript error. For example:

    this..ws.on("new", (response) => {

      this._list= response.list;
     this._listSubjet$.next(this._list);

    });
triniwiz commented 8 years ago

what platform are you using ?

xLama commented 8 years ago

@triniwiz More information:

    "tns-android": {
      "version": "2.1.1"
    }
  },
  "dependencies": {
    "@angular/common": "2.0.0-rc.3",
    "@angular/compiler": "2.0.0-rc.3",
    "@angular/core": "2.0.0-rc.3",
    "@angular/http": "2.0.0-rc.3",
    "@angular/platform-browser": "2.0.0-rc.3",
    "@angular/platform-browser-dynamic": "2.0.0-rc.3",
    "@angular/platform-server": "2.0.0-rc.3",
    "@angular/router": "3.0.0-alpha.7",
    "email-validator": "1.0.4",
    "nativescript-angular": "0.2.0",
    "nativescript-social-share": "1.2.0",
    "nativescript-socketio": "^2.1.2",
    "tns-core-modules": "2.1.0"
  },

Nativescript + Angular2 RC.3. I am using Observables and Observers but with Subject. I have a service to request server for information. In the other hand, that service uses a WebSocket to recieve more information.


 private _woList$: Subject<WO[]>;
  private _woList: WO[];

  constructor(private httpService: HttpService, private socketService: SocketService) {
    this._woList$ = new Subject<WO[]>();
  }

  public get woList$() {
    return this._woList$.asObservable();
  }

  public readMany(callBack?) {
    this.httpService.get("ots/?expand=ot,materials,tools,username", callBack || (response => {
      this._woList = response.ots;
      this.emitChanges();
    }));
  }
   emitChanges() {
    this._woList$.next(this._woList);
  }

  public listenSocketEvents() {
    this.socketService.ws.on("newWO", (wo) => {
      this.readMany();
    });
  }

wo.component.ts


  public ngOnInit() {
    this.woList = this.wotService.woList$;
  }

Well, if a button has a tap event witch execute emitChanges(), it works nice. And the httpService.get callback workd nice too. But it does not work in callback of socket. I dispatch socket event from server and it arrives to frontend perfectly. So, socket callback is executed. I don´t know if is a Nativescript issue or not.

donburgess commented 8 years ago

Your use of the plugin is pretty simple and should work... We'd need a better description of the problem you're going through. Sounds like a lot of different breakdowns could be happening including your socket server not emitting to the socket.

You don't seem to be calling listenSocketEvents() anywhere as well.

xLama commented 8 years ago

Thanks @db3dev

Server is emitting. I am using a Android Emulator but now I am going to try it in a device. It´s very strange. Now my code is following:

    this.woService.woList$.subscribe(m => {
      console.log("List size before: " + this.woList.length)
      this.woList = m;
      console.log("List size after: " + this.woList.length)
    },
      error => {
        console.log(error);
      });

Server emit a newWO event and it arrives to front. woList$ emits a change and console.log is working but. I can see:

List size before: 0 List size after: 1 (this one is the new one emitted by socket)

but screen does not show changes.

For this reason it´s probably a Nativescript issue, not a nativescript-socketio one.

donburgess commented 8 years ago

I'd recommend checking into your angular bindings and how you're rendering the view.