triniwiz / nativescript-socketio

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

Error with feathersjs socketioclient #54

Closed arkenstan closed 6 years ago

arkenstan commented 6 years ago

Hi I use feathersjs for real-time backend it provides a package for client called featherjs/socketio-client which require socket.io-client io property which is not working with nativescript socketio.

Please help

moayadnajd commented 6 years ago

@arkenstan i am using feathersjs also and i use this plugin and it seems working fine you have to check some things 1- you have to use the ip address of your device not the localhost since the emulator is not same device

   imports: [
        NativeScriptModule,
        AppRoutingModule,
        SocketIOModule.forRoot('http://192.168.1.4:3030'), // not localhost :D

    ],

2- import below libraries for more info see this but you have to keep in mind some functions will not work like the create()

const feathers = require('@feathersjs/feathers');
const socketio = require('@feathersjs/socketio-client');

3- you have to pass the socket to the feathers client like below

export class ItemsComponent implements OnInit {
    items: Item[];

    // This pattern makes use of Angular’s dependency injection implementation to inject an instance of the ItemService service into this class. 
    // Angular knows about this service because it is included in your app’s main NgModule, defined in app.module.ts.
    constructor(
        private itemService: ItemService,
         private socketIO: SocketIO,
         private ngZone: NgZone

        ) {
    }

    ngOnInit(): void {
        this.items = this.itemService.getItems();
        this.socketIO.connect();
        this.socketIO.on('connect',(io)=>{
            console.log('connect');
        });
        const app = feathers();
             // Set up Socket.io client with the socket
        app.configure(socketio(this.socketIO));
        // Receive real-time events through Socket.io
        app.service('items').on('created', item => 
        {
            console.log('created');
            this.items.push(item)
        }       
       );
    }

let me know if its work for you

arkenstan commented 6 years ago

Hi @moayadnajd thanks for the reply I tried this plugin following your instructions

import { Injectable, OnInit } from '@angular/core';

import * as feathersRx from 'feathers-reactive';
import { SocketIO } from 'nativescript-socketio';
import localStorage from 'nativescript-localstorage';

import feathers from '@feathersjs/feathers';
import feathersSocketIOClient from '@feathersjs/socketio-client';
import feathersAuthClient from '@feathersjs/authentication-client';

@Injectable()
export class FeatherService implements OnInit{
    private _feathers = feathers();
    constructor(private socketIO:SocketIO ){
    }
    ngOnInit(){
        this.socketIO.connect();
        this.socketIO.on('connect', (io) => {
            console.log('connect');
        });
        this._feathers
        .configure(feathersSocketIOClient(this.socketIO))
        .configure(feathersAuthClient({
            storage:localStorage
        }))
        .configure(feathersRx({
            idField:'_id'
        }));
    }

    public service(name){
        return this._feathers.service(name);
    }
}

but on running the project it gives an error:

app/services/feathers.service.ts(22,43): error TS2345: Argument of type 'SocketIO' is not assignable to parameter of type 'Socket'.
  Property 'io' is missing in type 'SocketIO'.
moayadnajd commented 6 years ago

@arkenstan this is a typescript error not runtime error i think you just need to do it without strictly type it

arkenstan commented 6 years ago

@moayadnajd thanks for helping, I successfully the ran the project. But there are still some things I can't do like using feathers-reactive to create watch. Is there a way to get original socket.io-client working in TNS