0

I watched some videos and I made a chat app with a socket.io server . Here is my server code :

const app = require("express")();

const server = require("http").createServer(app);

const io = require("socket.io")(server, {
  cors: {
    origin: "*",
  },
});

io.on("connection", (socket) => {
  
  socket.on("chat", (payload) => {
    console.log("What is payload", payload);
    socket.broadcast.emit("chat", payload);
  });
});

// app.listen(5000, () => console.log("server is active..."));

server.listen(5000, () => {
  console.log("Server is listening at port 5000...");
}); 

Now, my question is if this is possible without a server. I just want to show some dummy text with socket.io . My server is on the port 5000 and my localhost on 8080 . Is it possible to make an offline version with a setTimeout to get the dummy text?

  • Sure, write a script that acts as client and sends messages. If you want a real unit test, see [Unit testing Node.js and WebSockets (Socket.io)](https://stackoverflow.com/questions/15509231/unit-testing-node-js-and-websockets-socket-io/64485931#64485931) – ggorlen Jul 23 '21 at 23:32
  • I am new to this, so I don't quite understand. I know how to make it with a server and the app is working. I don't understand how to emit a message from a script . I was using : socket.emit("chat", { message, userName,received:true }); and I was receiving in the useEffect. In use effect, i used "socket.on" to get the data. Can I still use socket on without a server? – StillLearning Jul 23 '21 at 23:39
  • Maybe I misunderstood your post. How are you supposed to run an express app without a server? Or do you mean you want to mock the socket for a front-end React project so you can run _that_ without a server? Maybe try [How to test/mock socket.io client](https://stackoverflow.com/questions/63605899/how-to-test-mock-socket-io-client) – ggorlen Jul 23 '21 at 23:53
  • I just started with an internship program 2 weeks ago. I made a full application with a server but I was told that I don't need the backend, I just need the frontend socket and a service that would send some dummy data instead of the backend. Thank you, I will try something similar, i don't need to test (jest or testing library) , I need a setTImeout function that will send the dummy data. That's why I am confused how to make it without a server... – StillLearning Jul 24 '21 at 00:34

0 Answers0