How to integrate socket in loopback 4 server API #7175
Replies: 67 comments 2 replies
-
Hi @imranalisolanki can you share a minimal version of your app reproducing the issue, so I can troubleshoot? Sharing a code snippet is not enough, since other factors responsible for the failure may be left out of the context. |
Beta Was this translation helpful? Give feedback.
-
@hacksparrow i have create a simple loopback4 server with some routing and i have gone through https://github.com/raymondfeng/loopback4-example-websocket/ this example and when i have configure it in my server then existing routing is not working. import { TokenServiceBindings, UserServiceBindings, TokenServiceConstants, PasswordHasherBindings, EmailServiceBindings, FCMServiceBindings, ControllerServiceBindings } from './keys'; export interface PackageInfo { const pkg: PackageInfo = require('../package.json'); export class BreezeApiApplication extends BootMixin(ServiceMixin(RepositoryMixin(RestApplication))) {
} setUpBindings(): void {
} index.ts import { BreezeApiApplication } from './application'; export { BreezeApiApplication }; export async function main(options: ApplicationConfig = {}) { app.io = require('socket.io')(await app.start());
}); const url = app.restServer.url; return app; |
Beta Was this translation helpful? Give feedback.
-
@imranalisolanki the code you pasted refers to many files not found in the original https://github.com/raymondfeng/loopback4-example-websocket/. Also it has basic run-time errors like I will be able to help you if you can provide a minimal app reproducing the error. Code snippets are not enough. |
Beta Was this translation helpful? Give feedback.
-
I have the same problem as @imranalisolanki. I would also like to know how to use/implement the websocket from this example https://github.com/raymondfeng/loopback4-example-websocket/ in this example https://github.com/strongloop/loopback4-example-shopping . |
Beta Was this translation helpful? Give feedback.
-
I have been following this topic for a long time. Everyone who has done it successfully with https://github.com/strongloop/loopback4-example-shopping. |
Beta Was this translation helpful? Give feedback.
-
Hi guys, I had the same problem: Looback 4 RestApplication + socket.io. I solved it based on loopback4-example-todo-list and loopback4-example-websocket. Here is the partial codes:
I am starting with LB4, and I don't know what are the future repercussions of this implementation or if it is a bad practice, but, at the moment, it works for me and maybe for you too. -------------------------- Updated --------------------- To inject dependencies of the main app into websocket controllers you must inherit the websocket context of the main context
|
Beta Was this translation helpful? Give feedback.
-
thanks @arondn2 . I will try to integrate it into my program |
Beta Was this translation helpful? Give feedback.
-
Hi @arondn2 . when i integrate in my program i am getting error. |
Beta Was this translation helpful? Give feedback.
-
hi @imranalisolanki. I had a similar problem after my first comment in this feed. It is because websocket has not the same context of app. If you have a regular loopbak 4 app like loopback4-example-todo-list you must inherit the websocket context of the main context. I did with this changes.
-------------------------- Updated --------------------- The links were wrong. I have updated them. |
Beta Was this translation helpful? Give feedback.
-
@arondn2 httpserver and websocketserver must have different ports ? I get index.ts used from src/index.ts |
Beta Was this translation helpful? Give feedback.
-
@d-bo I successfully handled the web when I changed port 5000 to a different port than the http server.
|
Beta Was this translation helpful? Give feedback.
-
@xdien just commented out original http server from skeleton app. finally both websocket and http server are on the same port: // src/index.ts
// ...
export async function main(options: ApplicationConfig = {}) {
const app = new PrjApplication(options);
await app.boot();
// commented out original http server
//await app.start();
await app.startWebSocket();
const url = app.restServer.url;
console.log(`Server is running at ${app.httpServer.url}/api`);
return app;
}
// ... |
Beta Was this translation helpful? Give feedback.
-
I didn't need to comment on that line. I made a complete example with the integration: loopback4-example-websocket-app. I hope it helps |
Beta Was this translation helpful? Give feedback.
-
I try to inject websocket controller into
Seems it is not the right way doing that:
|
Beta Was this translation helpful? Give feedback.
-
@d-bo why are you trying to inject a controller into another controller? I assume notification.controller is a regular rest controller to handle HTTP requests, and WebSocketController is to handle connections socket.io. WebSocketController need the socket instance from socket.io connection which doesn't exist in a HTTP request. |
Beta Was this translation helpful? Give feedback.
-
@alexkander Did you find out the problem of "port in use" issue? I found that the key is the value of "host" in config. The value of "host" for "rest" is 'localhost' and for websocket is undefined. If you assign "locahost" to the host of websocket, you have able to repo the issue. I still have no idea for the solution. |
Beta Was this translation helpful? Give feedback.
-
@justin65 I couldn't even replicate the problem. If you could create a repo test with the minimum configuration that generates the error, I could review it. |
Beta Was this translation helpful? Give feedback.
-
@alexkander I just fork the project and add a commit to replicate the problem. |
Beta Was this translation helpful? Give feedback.
-
@justin65 with your repository I can replicate the problem. I'll take a look as soon as I can |
Beta Was this translation helpful? Give feedback.
-
How can I create a REST and socket.io application? |
Beta Was this translation helpful? Give feedback.
-
@alexkander Did you have any update? Should both rest and socket.io share the same http server instance? Thanks. |
Beta Was this translation helpful? Give feedback.
-
@justin65 your fork is of a old repository. You should to use the example of socketio that currently loopback 4 has. @loopback/example-socketio. I gonna to archive my example repository because it is no longer stable. Anywhere, do you need set websocket: {
port,
// host: 'localhost',
} I gonna try to work in the documentation of the example and extension of socketio as soon as I can. Sorry for not being able to help more. Regards |
Beta Was this translation helpful? Give feedback.
-
@alexkander I know that loopback 4 release a socketio example. But is't an Application, I can not extend it from rest application like the repository you are going to archive. That's why I keep working on old repository. Is there any guideline to merge two loopback 4 application? Regards |
Beta Was this translation helpful? Give feedback.
-
@aleksspeaker I'am trying to use host in websocket config but it leads to a problem |
Beta Was this translation helpful? Give feedback.
-
@justin65 , @ArtemShapovalov guys there is a working websocket repo built from original lb4 skeleton app https://github.com/d-bo/lb4-client-err where And the line causing this error https://github.com/d-bo/lb4-client-err/blob/173b996da0974f2a39f20ac0c53f67d95be0c29b/src/index.ts#L14 |
Beta Was this translation helpful? Give feedback.
-
@d-bo thanks a lot it solve problem with
|
Beta Was this translation helpful? Give feedback.
-
@alexkander |
Beta Was this translation helpful? Give feedback.
-
That's something we should fix for |
Beta Was this translation helpful? Give feedback.
-
Improved version https://github.com/alexkander/loopback4-example-websocket-app |
Beta Was this translation helpful? Give feedback.
-
My improved version of websocket application with extended RestApplication. |
Beta Was this translation helpful? Give feedback.
-
i want implement socket in my loopback4 server API. i have integrate socket in my API but it;s routing is not working.
it's my code but socket is not connected. i have seen this example.
https://github.com/raymondfeng/loopback4-example-websocket/
but it's work only socket server not working with API server. please suggest me how i can implement.
Beta Was this translation helpful? Give feedback.
All reactions