Welcome to the Linux Foundation Forum!

Cannot get the WebSockets example working

Posts: 1
edited June 2024 in LFW111 Class Forum

Hello,
I'm running the LFW111 course examples on node 20.11.0 with

  1. "dependencies": {
  2. "@fastify/autoload": "^5.0.0",
  3. "@fastify/cors": "^9.0.1",
  4. "@fastify/sensible": "^5.0.0",
  5. "@fastify/websocket": "^10.0.1",
  6. "fastify": "^4.26.1",
  7. "fastify-cli": "^6.2.1",
  8. "fastify-plugin": "^4.0.0"
  9. },

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:

  1. [16:45:38.506] ERROR (369197): Cannot read properties of undefined (reading 'send')
  2. reqId: "req-2"
  3. err: {
  4. "type": "TypeError",
  5. "message": "Cannot read properties of undefined (reading 'send')",
  6. "stack":
  7. TypeError: Cannot read properties of undefined (reading 'send')
  8. at Object.<anonymous> (file:///home/cfinis/workspace/jsnad/my-project-WS/mock-srv/routes/orders/index.mjs:9:16)
  9. at /home/cfinis/workspace/jsnad/my-project-WS/mock-srv/node_modules/@fastify/websocket/index.js:185:34
  10. ...
  11. }

Any idea what is going wrong?

Thanks

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Answers

  • I've got the same problem. Did you ever get it working?

  • @jonatasmello , thank you! That was the problem.

  • Saved my sanity with this, thanks!

  • 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

  • @jonatasmello Thanks it worked.

  • @jonatasmello said:
    Hi,
    here is what worked for me based on the official documentation

    async ({ socket }, request) => {

    use this instead:

    async ( socket, request) => {

    the parameter "socket" is just a variable, not an object (with {socket} )

    1. Let's also create an index.mjs file in our newly created folder with the following contents:
    2.  
    3. "use strict";
    4.  
    5. export default async function (fastify, opts) {
    6. fastify.get(
    7. "/:category",
    8. { websocket: true },
    9. async (socket, request) => {
    10. socket.send(JSON.stringify({ id: "A1", total: 3 }));
    11. }
    12. )};

    This is the miracle working code!! A crack!!

  • Posts: 1

    Grazie!

  • Posts: 2

    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.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training