Welcome to the Linux Foundation Forum!

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

  • aldaek
    aldaek Posts: 2

    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?

  • ntrade
    ntrade Posts: 1

    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

Categories

Upcoming Training