我想从我的快递应用程序向客户发送实时通知。但我不确定如何实际使用 socket.io。如何将其与最佳实践相结合。
mongoose
.connect(process.env.MongoDb_Url)
.then(() => {
const io = socket(
app.listen(5000, () => console.log("Server And Database Are Running")),
{
cors: {
origin: "http://localhost:3000",
},
}
);
io.on("connection", (socket) => {
socket.on("message", async (message) => {
const messageCreated = await chatModel.create({ ...message });
socket.emit("createdMessage", messageCreated);
});
});
})
.catch((error) => console.log(error.message));
所以我在连接数据库后使用 socket.io 。
OR
如果 (2) 是这种情况,请给我有关如何执行此操作以及如何正确使用 socket.io 的代码。谢谢