alexkander / loopback4-example-websocket-app Goto Github PK
View Code? Open in Web Editor NEWLoopback 4 minimal app with websocket
License: Other
Loopback 4 minimal app with websocket
License: Other
how to get message from /todos/room/example/emit/ in angular!!
Thanks for the upload, it took me a while to find it :)
it has a quirk it will only accept socket.io connection on localhost
the server accepts http requests on all interfaces (127.0.0.1 and 192.168.1.x) but sockets only work if connection is made to ws://localhost:3000
when i run this code
Cannot start the application. Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1313:16)
at listenInCluster (net.js:1361:12)
at Server.listen (net.js:1447:7)
at HttpServer.start (/home/georman/Documents/loopback4-example-websocket-app/node_modules/@loopback/http-server/dist/http-server.js:50:21)
at WebSocketServer.start (/home/georman/Documents/loopback4-example-websocket-app/dist/websockets/websocket.server.js:61:31)
at TodoListApplication.start (/home/georman/Documents/loopback4-example-websocket-app/dist/websockets/websocket.application.js:17:29)
at main (/home/georman/Documents/loopback4-example-websocket-app/dist/index.js:14:15) {
code: 'EADDRINUSE',
errno: 'EADDRINUSE',
syscall: 'listen',
address: '::',
port: 3000
}
When I call chat.html, I got 404 error with websocket request.
Adapting this example to make it function in my own application result in an adress collision error.
While this is resolved, I wanted to expose the fix.
My environment :
npm start
result in an EADDRINUSE: address already in use :::3000
kind of error. Doing the same with the example work as expected.Temporary fix : In index.ts
, map the rest
and the websocket
endpoints to two differentes ports.
Definitive fix :
websocket.application.ts
, comment out the super.*
calls in start
and stop
websocket.server.ts
, modify the constructor as such :constructor(
public ctx: Context,
public readonly httpServer: HttpServer,
private options: ServerOptions = {} as any,
) {
super(ctx);
this.io = new Server(options);
ctx.bind('ws.server').to(this.io);
}
with Server
already imported from socket.io
(see line 3 on this example).
index.ts
, modify the url constant to match httpServer
instead of restServer
So I have a loopback 4 application and followed your example to have a working websocket.
Right now I am trying to have a cronjob and emit message through the websocket but it seems I cant get it to work.
Followed the cronjob example here cronjob
I just need to be able to access the websocket from inside the cronjob but its returning the out-of-context error.
Appreciate the help if you could.
import { createBindingFromClass, inject } from '@loopback/core';
import {CronJob, cronJob} from '@loopback/cron';
@cronJob()
export class MyCronJob extends CronJob {
constructor(
) {
super({
name: 'myEventsCronJob',
onTick: () => {
// do the work
this.runJobOnTick();
},
cronTime: '*/40 * * * * *', // Every ten second
start: false,
});
}
runJobOnTick() {
//console.log('cool, cronjob is running');
// emit message where, example , socket.emit() or something
}
}
export const cronJobBinding = createBindingFromClass(MyCronJob);
If the connection is abnormally terminated on the client, then on the server I start receiving messages: "socket.io: client ignoring packet write ...". The client only receives every second packet, full restarting the client doesn't help.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.