Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //using express
- var express = require("express");
- //creating instance of express
- var app = express();
- //using http with the instance of express
- var http = require("http").createServer(app);
- //creating socket.io instance with http
- var io = require("socket.io")(http);
- //creating listeners for new connections
- io.on("connection", (socket) => {
- console.log("User connected: ", socket.id);
- //emitting a message to each user once connected
- io.to(socket.id).emit("new_message", {id: "999", msg: "Welcome back, your Id is " + socket.id});
- })
- //here I want to emit a message to a user by the URL params, the Id is the socket Id and the message is the message body, this is a non-socket proccess and therefore I need to use socket.io-emitter and this is where I'm having problems
- app.get("/send/:id/:message", (request, result) => {
- io.to(request.params.id).emit({ id: "999", msg: request.params.message });
- result.send("Message Emitted Via API Call, Send to Id = " + request.params.id)
- })
- //creating the server
- const port = 3001;
- http.listen(port, () => {
- console.log("Listening to port " + port);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement