When I test socketio in moleculer get `404 Not Found` error

67 views Asked by At

I created a socketio demo as below:

services/socketiodemo.service.js

"use strict";

const ApiGateway = require("moleculer-web");

// require Socket.IO and Moleculer's Socket.IO mixin
const SocketIOService = require("moleculer-io");


module.exports = {
  name: "socketiodemo",

  mixins: [ApiGateway, SocketIOService],

  settings: {
    io: {
      path: "/socket.io",
      cors: true 
    }
  },

  events: {
    // handle connections of clients
    "socket.connected" (ctx) {
      console.log(`Socket ${ctx.socket.id} connected to ${ctx.nodeID}.` );
      ctx.emit("hello", "Connected successfully!");
    },

    "socket.disconnected" (ctx) {
      console.log(`Socket ${ctx.socket.id} disconnected from ${ctx.nodeID}`)
    },
    // handle incoming message from the connected client
    "socket.message" (ctx, data) {
      console.log(`Socket ${ctx.socket.id} sent message: ${data}`);
      // broadcast the message to all connected clients
      ctx.broadcast("message", data);
  },


  },

}

now I use html client for testing:

<!DOCTYPE html>

<html>
    <head>
        <title>Socket.IO Client Example</title>
        <script src="https://cdn.socket.io/socket.io-3.0.3.min.js"></script>

        <script>
            // create socket.io client connection
            const socket = io("http://localhost:3000", {
                path: "/socket.io",
            });

            // handle incoming message from server
            socket.on("message", function (message) {
                console.log("Incoming message: ", message);
            });

            // handle incoming message from server when connected
            socket.on("hello", function (message) {
                console.log("Connected to server: ", message);
            });

            // sendmessage to server when button is clicked
            function sendMessage() {
                const input = document.getElementById("message-input");
                const message = input.value;

                socket.emit("message", message);
                input.value = "";
            }
        </script>
    </head>
    <body>
        <div>
            <input
                type="text"
                id="message-input"
                placeholder="Enter message..."
            />
            <button onclick="sendMessage()">Send</button>
        </div>
    </body>
</html>

but get 404 Not Found error: enter image description here enter image description here

1.please tell me why there get this issue?

2.and I check the localhost:3000 also not find the /socket.io (only /list-aliases API), why don't have /socket.io? and whether this is related to first question?

enter image description here

1

There are 1 answers

0
nvvetal On

Maybe need nginx config or correct route set in node.js (express etc) like

    location /socket.io/ {
        proxy_pass http://socket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;

    }