SHARE
TWEET

Untitled

a guest Jan 24th, 2020 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //using express
  2. var express = require("express");
  3. //creating instance of express
  4. var app = express();
  5. //using http with the instance of express
  6. var http = require("http").createServer(app);
  7. //creating socket.io instance with http
  8. var io = require("socket.io")(http);
  9.  
  10. //creating listeners for new connections
  11. io.on("connection", (socket) => {
  12.     console.log("User connected: ", socket.id);
  13.     //emitting a message to each user once connected
  14.     io.to(socket.id).emit("new_message", {id: "999", msg: "Welcome back, your Id is " + socket.id});
  15. })
  16.  
  17. //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
  18. app.get("/send/:id/:message", (request, result) => {
  19.     io.to(request.params.id).emit({ id: "999", msg: request.params.message });
  20.     result.send("Message Emitted Via API Call, Send to Id = " + request.params.id)
  21. })
  22.  
  23. //creating the server
  24. const port = 3001;
  25. http.listen(port, () => {
  26.     console.log("Listening to port " + port);
  27. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top