Receiving error in Enhancing an HTTP Server with WebSockets (4)
I'm getting the following error and the web page reports orders: pending on all items. I went back and copy/pasted exactly the files and I'm still getting the error. Please, any help will be appreciated. I even made sure to do the npm install @fastify/websocket earlier in the mock-serv subfolder.
[13:46:09.576] ERROR (1548187): socket.send is not a function
reqId: "req-2"
req: {
"method": "GET",
"url": "/orders/electronics",
"host": "localhost:3000",
"remoteAddress": "::1",
"remotePort": 57220
}
res: {
"statusCode": 500
}
err: {
"type": "TypeError",
"message": "socket.send is not a function",
"stack":
TypeError: socket.send is not a function
at Object. (file:///home/allen/Repos/LFW111/starting/mock-srv/routes/orders/index.mjs:8:14)
at preHandlerCallbackInner (/home/allen/Repos/LFW111/starting/mock-srv/node_modules/fastify/lib/handleRequest.js:161:24)
at preHandlerCallback (/home/allen/Repos/LFW111/starting/mock-srv/node_modules/fastify/lib/handleRequest.js:129:5)
at validationCompleted (/home/allen/Repos/LFW111/starting/mock-srv/node_modules/fastify/lib/handleRequest.js:119:5)
at preValidationCallback (/home/allen/Repos/LFW111/starting/mock-srv/node_modules/fastify/lib/handleRequest.js:96:5)
at handler (/home/allen/Repos/LFW111/starting/mock-srv/node_modules/fastify/lib/handleRequest.js:73:7)
at Object.handleRequest (/home/allen/Repos/LFW111/starting/mock-srv/node_modules/fastify/lib/handleRequest.js:29:5)
at runPreParsing (/home/allen/Repos/LFW111/starting/mock-srv/node_modules/fastify/lib/route.js:571:19)
at next (/home/allen/Repos/LFW111/starting/mock-srv/node_modules/fastify/lib/hooks.js:236:9)
at /home/allen/Repos/LFW111/starting/mock-srv/node_modules/@fastify/cors/index.js:209:12
}
Comments
-
This is happening with both Node version 22.15.0. Upon running Node version 18.16.1 (npm run dev) provides the following: Warn: unable to load fastify module. Please help. What am I doing wrong on this excercise?
Do I need to display/cat my files here?
0 -
version-proof handler:
"use strict";
module.exports = async function (fastify, opts) {
fastify.get(
"/:category",
{ websocket: true },
async (connection, request) => {
const socket = connection.socket || connection;
for(const order of fastify.currentOrders(request.params.category)){
socket.send(order)
};for await (const order of fastify.realtimeOrders()){ if(socket.readyState >= socket.CLOSING) break; socket.send(order); } },);
};Old versions → connection is the WebSocket
New versions → connection.socket is the WebSocket
This line handles both:
const socket = connection.socket || connection
0
Categories
- All Categories
- 177 LFX Mentorship
- 177 LFX Mentorship: Linux Kernel
- 750 Linux Foundation IT Professional Programs
- 373 Cloud Engineer IT Professional Program
- 169 Advanced Cloud Engineer IT Professional Program
- 74 DevOps IT Professional Program - Discontinued
- 4 DevOps & GitOps IT Professional Program
- 99 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- 1 AI & ML Training
- 1 Blockchain & Decentralized Identity Training
- 4 Cloud & Containers Training
- 1 Cybersecurity Training
- 2 DevOps & Site-Reliability Training
- 1 Linux Kernel Development Training
- 1 Networking Training
- 2 Open Source Best Practice Training
- 1 System Administration Training
- 1 System Engineering Training
- 1 Web & Application Development Training
- 792 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 95 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 87 Storage
- 769 Linux Distributions
- 81 Debian
- 68 Fedora
- 22 Linux Mint
- 13 Mageia
- 24 openSUSE
- 150 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 465 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 98 Linux Security
- 78 Network Management
- 101 System Management
- 46 Web Management
- 106 Mobile Computing
- 18 Android
- 73 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 392 Off Topic
- 121 Introductions
- 181 Small Talk
- 29 Study Material
- 955 Programming and Development
- 310 Kernel Development
- 627 Software Development
- 983 Software
- 375 Applications
- 182 Command Line
- 5 Compiling/Installing
- 68 Games
- 317 Installation
- Archived
- 2 LFD140 Class Forum
- 1.4K LFS258 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)