Cannot get the WebSockets example working
Hello,
I'm running the LFW111 course examples on node 20.11.0 with
"dependencies": {
"@fastify/autoload": "^5.0.0",
"@fastify/cors": "^9.0.1",
"@fastify/sensible": "^5.0.0",
"@fastify/websocket": "^10.0.1",
"fastify": "^4.26.1",
"fastify-cli": "^6.2.1",
"fastify-plugin": "^4.0.0"
},
The Web Sockets example in lesson 04 (Going Realtime) doesn't work for me. When I run the mock-service, and select a category in the static web page, the mock service of the routes/orders plugin throws an error complaining that the socket is undefined:
[16:45:38.506] ERROR (369197): Cannot read properties of undefined (reading 'send')
reqId: "req-2"
err: {
"type": "TypeError",
"message": "Cannot read properties of undefined (reading 'send')",
"stack":
TypeError: Cannot read properties of undefined (reading 'send')
at Object.<anonymous> (file:///home/cfinis/workspace/jsnad/my-project-WS/mock-srv/routes/orders/index.mjs:9:16)
at /home/cfinis/workspace/jsnad/my-project-WS/mock-srv/node_modules/@fastify/websocket/index.js:185:34
...
}
Any idea what is going wrong?
Thanks
Answers
-
I've got the same problem. Did you ever get it working?
0 -
Hi,
here is what worked for me based on the official documentationasync ({ socket }, request) => {use this instead:
async ( socket, request) => {the parameter "socket" is just a variable, not an object (with {socket} )
Let's also create an index.mjs file in our newly created folder with the following contents: "use strict"; export default async function (fastify, opts) { fastify.get( "/:category", { websocket: true }, async (socket, request) => { socket.send(JSON.stringify({ id: "A1", total: 3 })); } )};9 -
@jonatasmello , thank you! That was the problem.
0 -
Saved my sanity with this, thanks!
0 -
Thanks @jonatasmello. Could the admins fix this? Also, there is a minor bug on the related page https://trainingportal.linuxfoundation.org/learn/course/introduction-to-nodejs-lfw111x/going-real-time/real-time-functionality?page=4 -> the static files should be served with command npx serve -p 5050 static
0 -
@jonatasmello Thanks it worked.
0 -
@jonatasmello said:
Hi,
here is what worked for me based on the official documentationasync ({ socket }, request) => {use this instead:
async ( socket, request) => {the parameter "socket" is just a variable, not an object (with {socket} )
Let's also create an index.mjs file in our newly created folder with the following contents: "use strict"; export default async function (fastify, opts) { fastify.get( "/:category", { websocket: true }, async (socket, request) => { socket.send(JSON.stringify({ id: "A1", total: 3 })); } )};This is the miracle working code!! A crack!!
0 -
Grazie!
0 -
Muchas gracias. Funciona correctamente con los datos originales, pero en cuanto añades nuevos registros estos quedan permanentemente sin definir. Entiendo que es lo normal dado que es un simple ejemplo.
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
- 984 Software
- 376 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)
