-
Notifications
You must be signed in to change notification settings - Fork 1.1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
How to integrate socket in loopback 4 server API #4044
Comments
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. |
@arondn2 i need to emit incoming data from rest
tried inject only socket
no luck) same |
@d-bo I think you have to implement a room scheme like socket.io explains in their documentation here. I added some code in loopback4-example-websocket-app repo to show you a way to implement it, specifically how to emit an event from TodoController (Http request controller) to socket.io room. The specifics changes are:
|
Thanks for sharing the updated repo. I just downloaded your updated version and it looks like we still have an issue with "port in use". `> [email protected] prestart /home/zzz/latesttry/loopback/lb4/loopback4-example-websocket-app
Cannot start the application. Error: listen EADDRINUSE: address already in use 127.0.0.1:3000 npm ERR! A complete log of this run can be found in:
Will appreciate if you can please look into it. Thanks |
@argupta23 Sorry, i can't get this issue but I am looking at what is happening. |
I am in the same situation as you and trying to achieve something similar. Yes, I did what you suggested but have the same result. I even tried the suggestion that was earlier made to comment out app.start(), but that made matters worse. To get around the issue, within index.ts, I appended the port with 5000 and it seems to comeup. But in this case we now have the app running on 2 ports (3000 and 5000). Hope this helps. Please let me know if you want me try something else. |
@argupta23 I will comment this when I find out the issue. |
@argupta23 I have run the example in 3 differents computers and it always run ok. I have noticed you haven't run |
@arondn2 no, i didn't clone the repo |
@d-bo, does your version comeup on a single port? Just ran a quick test again for you and here is the output. ubuntu: 20.04 running kernel 5.4.0-40-generic and package.json { sudo netstat -tulpn set websocket port to blank
Cannot start the application. Error: listen EADDRINUSE: address already in use 127.0.0.1:3000 npm ERR! A complete log of this run can be found in: sudo netstat -tulpn updated websocket port to 5000 loopback4-example-websocket-app$ vi src/index.ts loopback4-example-websocket-app$ npm start
Server is running at http://127.0.0.1:3000 ran netstat -tulpn from another terminal one thing to note is that the websocket is attached to an IPv6 address / port |
I can see you are using different ports for the configurations of rest and websocket. In my previous comment I say change the port for both, not just for websocket.
Also, it is very important to run Anyway I don't know how I can help you more. I still thinking there is process using that port. |
@arondn2 the point is to push notifications to user logged in from many devices, browsers. to put user into own socket.io room after successfull authentication . so i tried all the matched variants from socket.io cheatsheet |
@d-bo do you want to send the event to all others connections of the same authenticated user or of all users connected? |
@arondn2 to all other connections of the same authenticated user |
@d-bo I think if you are using If you want send the event to all other connections of the same authenticated user, you will need to use You need to create a minimal repository loopback project with this problem, because there's not much I can say to find out the problem if I can't have the entire view. |
Hi @arandn2 , I did what you said,but it keeps throwing me this error: |
@sergiogaitan I have say many things in this thread.. jajajajajajajaja... Please, tell me which of my comments you are talking about to try to help you. Otherwise, did you run the example that I left? |
@arondn2 Thanks for solving this issue, this example works great: https://github.com/arondn2/loopback4-example-websocket-app |
@arondn2 how to get |
regards, @d-bo |
Have anyone tried to implement the 'socket.io-client', loopback way? |
hi @w20k. Here is a example how to use socket.io-client for a acceptance test. https://github.com/strongloop/loopback-next/blob/socketio/extensions/socketio/src/tests/acceptance/socketio.server.acceptance.ts. I think you need to install socket.io-client and @types/socket.io-client dependencies |
Hi, @arondn2 thanks for a quick answer. Probably my issue is a bit more complicated :) I have a bidirectional connection via WebSocket where loopback is a middleman with (currently) Socket.io server (from the example). And now I need to figure out a proper way to initiate a connection to a server using the (as I've understood SocketIoServer doesn't support it?) 'socket.io-client', and somehow make those two websockets communicate, per user session_id. Communication part is not an issue, but to subscribe for client messages and push those on server. Or that's the only option, and I'm overthinking 😄 |
@w20k I'm not sure I understand. The scheme that you are trying to explain is something like this:
And are you trying send messages from (1) to (2) and those message from (2) to (3)? For this you need server.io-client in (2)? |
@arondn2 yeah, you got it right.
It's a WebSocket connection between (1) - (3), where (2) is a "Websocket manager", of some sort. And, yes, for this I was thinking I'd need to use 'server.io-client' in (2). |
@w20k if (3) is public or visible for (1) you can connect directly to (3). But I think (3) is private or maybe you just is required connect through (2). If this is the case, my opinion is you must implement a kind of proxy, where client in (1) connects to server in (2) and a client in (2) using socket.io-client in loopback connects to server in (3). Also (2) must catch events from client in (1) and emit them to server in (3) and catch events from server in (3) and emit them to (1). You could do the latter linking event by event, or you could use |
Thanks for a tip @arondn2. Minor note, who ever stumble on the same issue, after a day of debugging, for some reason (something related to: circular/connection issue) you cannot use 'server.io-client' from the NodeJS server (loopback4, in my case) to connect to internal/external server. It just doesn't let you (tried switching to transports: |
Guys, There is a new loopback 4 module for socketio @loopback/socketio and example @loopback/example-socketio. |
@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
|
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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.
The text was updated successfully, but these errors were encountered: