I have a chat app that I am trying to allow users an option to send a message from a url (rest api). This message should be send to other clients connect to the socket. Here is a little of the code. This is a nodeJs app.
User A & B are in a room. User C wants to send them a message from Rest Api http://localhost:3000/sendMessage?message=hello&roomId=123
import express from "express";
import { createServer } from "http";
import { Server } from "socket.io";
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
/* options */
});
io.on("connection", (socket) => {
app.get("/sendMessage", function (req, res) {
res.send("Message sent");
// This will send message to other clients when this endpoint is called
io.to("devices-in-this-room").emit("message", req.body.content);
});
});
httpServer.listen(3000);