Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to get Service instance outside the controller? #7

Closed
EugenePisotsky opened this issue Dec 10, 2016 · 6 comments
Closed

Comments

@EugenePisotsky
Copy link
Contributor

In ServerLoader for example. I need to configure socket.io and oauth2orize somewhere and i don't have any idea how to do it.
Or maybe you've got plans to make something like @configuration annotation in Spring Framework (Java)? It would be great!

P.S. Thanks for amazing library!

@Romakita
Copy link
Collaborator

Hello RevanScript,

Thanks for your compliments :)

Here a link to the wiki:
https://github.com/Romakita/ts-express-decorators/wiki/Class:-ServerLoader#serverloaderonready-void

You have the $onReady hook. It let you to configure your SocketIO:

class Server extends ServerLoader implements IServerLifecycle {

    public $onReady(): void {
        console.log('Server ready');
        const io = SocketIO(this.httpServer);
        // ...

    }
}

For oauth2orize I never used before.

@EugenePisotsky
Copy link
Contributor Author

Ok, i have a question about your option. If i'll initialize socket.io in the $onReady hook, how can i use it in controllers then?

@Romakita
Copy link
Collaborator

I confess that I did not push the problem so far. For me the controller is here to map the Express request to your method. Socket IO use Express to bind the server but I didn't use the Express method to create endpoint.

Do you have an example of what you would like to do with Socket IO and a controller?

@EugenePisotsky
Copy link
Contributor Author

EugenePisotsky commented Dec 10, 2016

I need socket.io instance, to emit notifications on requests. For example: user is sending request to /chat/message, then we insert message into the database and emit socket.io notification to other users.

Here i wrote a little example for this case:

@Service()
export class ChatService {

    io: SocketIO.Server; // socket.io instance
    
    constructor(public chatModel: ChatModel) {
        // constructor
    }

    postMessage(message: string) {
        this.chatModel.insertMessage(message);
        this.io.emit('message', message);
    }

}

@Romakita
Copy link
Collaborator

Ok I see :)

You can create your own SocketService like this:

@Service()
export default class SocketService {
      static io: SocketIO.Server;

      constructor() { 
      
      }

      // Maybe work
      public emit(...args) => SocketService.io.emit(...args);

      static createServer(httpServer)  {
           SocketService.io = SocketIO(httpServer);
      }
      
}

Then in your Server.ts

import SocketService from "./services/SocketService";

class Server extends ServerLoader implements IServerLifecycle {

    public $onReady(): void {
        
          SocketService.createServer(this.httpServer);

    }
}

And finally, In your controller or service you can inject SocketService to emit your event.

@Service()
export class ChatService {
    
    constructor(public chatModel: ChatModel, private socketService: SocketService) {
        // constructor
    }

    postMessage(message: string) {
        this.chatModel.insertMessage(message);
        this.socketService.emit('message', message);
    }

}

I think that will work :)

@EugenePisotsky
Copy link
Contributor Author

Yes, it's interesting idea. Thank you very much for the detailed answer! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants