question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

connection.send behaves differently on Fastify v4

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the regression has not already been reported

Last working version

3.29.0

Other packages

@fastify/websocket’: 6.0.1 ‘@fastify/cors’: 8.0.0

Stopped working in version

4.0.0

Node.js version

18.3.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Fedora 35

💥 Regression Report

I have this simple server:

const fastify = require("fastify")();
fastify.register(require("@fastify/websocket"));
fastify.register(require("@fastify/cors"));

fastify.get("/", { websocket: true }, (connection) => {
  console.log("Someone connected.");

  connection.socket.send("Hello from the server!");

  connection.socket.on("message", (message) => {
    console.log("Server was pinged: ", message.toString());

    connection.socket.send("Hello Fastify WebSockets");
  });
});

fastify.listen({ port: 9000 }, (err, address) => {
  if (err) {
    console.error(err);
    process.exit(1);
  }
  console.log(`Server listening at: ${address}`);
});

I run it using fastify@3.29.0 and it all works. I upgrade to version 4 and when I visit it on the client (http://localhost:9000):

{
  "statusCode": 500,
  "error": "Internal Server Error",
  "message": "connection.socket.send is not a function"
}

Same thing when I go to Postman and I connect via it’s websocket-type request (ws://localhost:9000). It just disconnects.

It seems to have a problem with connection.socket.send("Hello from the server!");. My goal is that i try to send some data from the server upon connection.

This use to work in fastify@3.x.x

Steps to Reproduce

  1. Run it using nodemon
  2. Observe terminal
  3. Connect to ws://localhost:9000 on Postman or connect to Client
  4. See no activity.
  5. Downgrade fastify to version 3.29.0. Observe success.

Expected Behavior

I expect the server to successfully connect and the fastify route / to start accepting socket messages.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
climba03003commented, Jun 21, 2022

I think it worth to update the document. Would you like to send a PR to address the issue?

1reaction
climba03003commented, Jun 21, 2022

I see the problem now. You must await the fastify.register(require("@fastify/websocket")).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Routes - Fastify
Additionally, to send a response before the request is handled by the ... The / route has different behavior depending on if the...
Read more >
Reaching Ludicrous Speed with Fastify - NearForm
Fastify behaves slightly differently to gain extra performance. fastify lifecycle diagram. When the server is started, Fastify carries out a ...
Read more >
Express vs Fastify: JSON processing | The JS runtimes - Medium
In this article, we'll do a comparison with JSON data, which is the defacto standard for data exchange over API calls.
Read more >
Fastify onSend hook not being called on web socket route
I'm filtering the data returning from a route before being sent back to the client using onSend hook and works as expected fastify....
Read more >
How to build a blazingly fast API with Fastify - LogRocket Blog
You can use nvm (Node Version Manager) for quickly switching between different Node.js versions. You'll also need a tool to send requests ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found