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. |
|
@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; |
|
@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. |
|
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 . |
|
I have been following this topic for a long time. Everyone who has done it successfully with https://github.com/strongloop/loopback4-example-shopping. |
|
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
|
|
thanks @arondn2 . I will try to integrate it into my program |
|
Hi @arondn2 . when i integrate in my program i am getting error. |
|
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. |
|
@arondn2 httpserver and websocketserver must have different ports ? I get index.ts used from src/index.ts |
|
@d-bo I successfully handled the web when I changed port 5000 to a different port than the http server. |
|
@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;
}
// ... |
|
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 |
|
I try to inject websocket controller into Seems it is not the right way doing that: |
|
@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. |
@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. |
|
@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. |
|
@alexkander I just fork the project and add a commit to replicate the problem. |
|
@justin65 with your repository I can replicate the problem. I'll take a look as soon as I can |
How can I create a REST and socket.io application? |
@alexkander Did you have any update? Should both rest and socket.io share the same http server instance? Thanks. |
|
@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 |
|
@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 |
|
@aleksspeaker I'am trying to use host in websocket config but it leads to a problem |
|
@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 |
|
@d-bo thanks a lot it solve problem with |
|
@alexkander |
|
That's something we should fix for |
|
Improved version https://github.com/alexkander/loopback4-example-websocket-app |
|
My improved version of websocket application with extended RestApplication. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
All reactions