Giter Site home page Giter Site logo

loopback4-example-websocket-app's People

Contributors

alexkander avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

loopback4-example-websocket-app's Issues

socket.io connection only works with localhost

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

ws and rest cannot be served in the same port

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
}

Impossibility to deploy both regular CRUD loopback server and Socket.io WS server on the same port [Resolved]

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 :

  • Loopback API is packaged on Docker container and has its port mapped
  • Socket.io is up-to-date as of now (v4.2.1) and Loopback-core is at v4.0.7.
    Error : Launching my LB app with 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 :

  • In websocket.application.ts, comment out the super.* calls in start and stop
  • In 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).

  • In index.ts, modify the url constant to match httpServer instead of restServer

How to have a cronjob that emit message through the websocket?

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);

Transfer problems after reconnect

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.