socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

docs: fix conjunction with fastify example

Open KartikeSingh opened this issue 8 months ago • 0 comments

The kind of change this PR does introduce

  • [ ] a bug fix
  • [ ] a new feature
  • [x] an update to the documentation
  • [ ] a code change that improves performance
  • [ ] other

Current behavior

Right now the readme example directly uses app.io in the conjunction with fastify example but io is undefined while the server (app) is not ready

const app = require('fastify')();
app.register(require('fastify-socket.io'));
app.io.on('connection', () => { /* … */ }); // here io is undefined
app.listen(3000);

New behavior

Now app.io is accessed after the server (app) is ready so it is defined

const app = require('fastify')();
app.register(require('fastify-socket.io'));
app.ready().then(() => {
    app.io.on('connection', () => { /* … */ }); // here app.io is defined
});
app.listen(3000);

Other information (e.g. related issues)

I was using it for the first time and got confused if I installed something wrong, then after diving into the detailed docs I understood what I was doing wrong. Couldn't find this issue on my first few google searches, so fixing the readme should save a lot of trouble for other fastify users

KartikeSingh avatar Jun 24 '24 05:06 KartikeSingh