-
-
Notifications
You must be signed in to change notification settings - Fork 286
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
Comments
Hello RevanScript, Thanks for your compliments :) Here a link to the wiki: 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. |
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? |
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? |
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);
}
} |
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 :) |
Yes, it's interesting idea. Thank you very much for the detailed answer! :) |
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!
The text was updated successfully, but these errors were encountered: