connection.send behaves differently on Fastify v4
See original GitHub issuePrerequisites
- 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
- Run it using
nodemon
- Observe terminal
- Connect to
ws://localhost:9000
on Postman or connect to Client - See no activity.
- 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:
- Created a year ago
- Comments:11 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think it worth to update the document. Would you like to send a PR to address the issue?
I see the problem now. You must
await
thefastify.register(require("@fastify/websocket"))
.