GithubHelp home page GithubHelp logo

fastify-ws's Issues

No way to listen for websocket disconnect

Hey,

I've tried to listen for when a websocket disconnects using:

 ws.on('connection', (socket) => {
       console.info("Someone connected!");
            socket.on('message', (data) => {
       });

       socket.on('disconnect', () => {
            console.info("someone disconnected");
       });
});

disconnect never gets called. Is there a way to know when someone disconnects?

NPM won't Install the module (Dependency fails to build)

utf-8-validate it's outdated and throws a 404 during the build

prebuild-install http 404 https://github.com/websockets/utf-8-validate/releases/download/v3.0.4/utf-8-validate-v3.0.4-node-v64-win32-x64.tar.gz

That causes to fail the entire installation.

EDIT: The issue is only present on node v10, works fine on the LTS release

Problems using it with TypeScript

fastify.ready(err => {
  if (err) throw err

  fastify.ws
    .on('connection', socket => {
      socket.on('message', msg => socket.send(msg)) // Creates an echo server
    })
})

Errors

[ts] Argument of type '(err: any) => void' is not assignable to parameter of type '() => void'.
(parameter) err: any

[ts] Property 'ws' does not exist on type 'FastifyInstance<{}, {}, {}>'.

Had to tweak it to the following to make it compile

fastify.ready(() => {
  fastify['ws']
    .on('connection', socket => {
      socket.on('message', msg => socket.send(msg)) // Creates an echo server
    })
})

Implementation v2.0

Hi,
I'm trying to implement your plugin in Fastify 2.0 but it's doesn't work.

fastify.ready(err => {
  if (err) throw err

  console.log('Server started.')

  fastify.ws
    .on('connection', socket => {
      console.log('Client connected.')

      socket.on('message', msg => socket.send(msg)) // Creates an echo server

      socket.on('close', () => console.log('Client disconnected.'))
    })
})

I don't see any error but my client can't connect to my websocket server.
Can you help me ?

thank you

On the ping interval handling

Hi! ๐Ÿ‘‹

Is the ping interval handling mandatory?
Use setInterval is not a very good idea, does not scale well and maybe the users do not need it.

Maybe expose a setKeepAlive function and let the user decide how handle this?

ECONNRESET

I get the following error message, just before disconnecting / Refreshing page / Socket Closing:

{"type":"Error","message":"read ECONNRESET","stack":"Error: read ECONNRESET\n at TCP.onread (net.js:622:25)","errno":"ECONNRESET","code":"ECONNRESET","syscall":"read"}

My code below:

Note that I first get the error message above and then my 'Client Disconnected' log message.

app.ready(err => {
    if (err) throw err

    app.log.info('Server started')

    app.ws.getUniqueID = function () {
        function s4() {
            return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
        }
        return s4() + s4() + '-' + s4()
    }

    app.ws
        .on('connection', socket => {
            socket.id = app.ws.getUniqueID()
            app.log.info('Client Connected: ' + socket.id)

            socket.on('message', msg => {
                socket.send(JSON.stringify({
                    from: socket.id,
                    to: socket.id,
                    msg: msg
                }))
            })

            socket.on('close', () => {
                app.log.info('Client Disconnected: ' + socket.id)
            })
        })
})

Please update for Fastify >= 1.x

I want to update to fastify 2.0 but this is the only plugin i use that is not ready for it. I tried to do a PR but I have no rights to do commits.

How I can open multiple sockets with fastify-ws?

Hi,

Currently, I'm running with a single socket. How I can open multiple socket connects with different routes. My single-socket example.

require('dotenv').config();
const fastify = require('fastify')({ logger: true });


fastify.register(require('fastify-cors'), {
  origin: '*',
});

fastify.register(require('fastify-ws')); // Registering websocket

function connectToSocket(ws, req) {
    console.log('Clinet got connected');
    let reqId = req.url;
 
    ws.on('message', (data) => {
      // handle messages that send from client to server
      data = JSON.stringify(data);
      console.log(data)
      ws.send(data);
    });
  }


/**
 * starting the server
 */
const start = async () => {
  try {
    await fastify.listen(process.env.APP_PORT || 8884, process.env.APP_HOST || '0.0.0.0');

    fastify.ws.on('connection', connectToSocket); // Initializing the socket controller
  } catch (error) {
    fastify.log.error(error);
    process.exit(1);
  }
};

start();

module.exports = fastify;

with this how I can open multiple socket connections. Please help.

Example

Hi,
I used forked repo from https://github.com/oransel/fastify-simple-ws and while plugin loads I can't test for any sent messages using https://amritb.github.io/socketio-client-tool/. I have a public POC repo https://github.com/johntom/Fastify-Demo2 and any help with sockets much appreciated.

  1. resgistration in plugins/websocket.js everything at first seems fine but I had to change
    fastify.register(require('fastify-ws'))
    to
    fastify.register(require('fastify-simple-ws'))

  2. I try to publish message in services/hello.js but socket.on does not fire
    I've only used sockets in a sails,js app and a test with feathers.js so I don't have tons of experiance in this area as both frameworks made it very easy
    Thanks,
    John

NPM Install warning

When I Install it, the following warning message appears:

npm WARN deprecated [email protected]: New code is available at github.com/uNetworking/uWebSockets.js

Would you mind if I prepare a PR for this?

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.