title: how to emit socket io to specific logged in user?
body: <p>I am using socket.io with express and typescript, when want to emit to particular logged in user it is not working, the rest is working fine, when new user join and other things, in App.ts in backend it looks like:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>httpServer.listen(8000, () =&gt; {
    console.log(`app is running on: http://localhost:8000`);
  });
  
  //SocketIO
  const io = new Server(httpServer, {
    cors: {
      credentials: true,
    },
  });
  
  app.set("socketio", io);
  
  io.use((socket: SocketWithUser, next: any) =&gt; {
    const token: any = socket.handshake.query.token;
  
    if (token) {
      try {
        const payload = jwt.verify(
          token,
          &lt;string&gt;process.env.JWT_TOKEN
        ) as DataStoredInToken;
  
        socket.userId = payload._id;
        return next();
      } catch (err) {
        next(err);
      }
    } else {
      return next(new Error("Access Denied"));
    }
  });
  io.on("connection", (socket: SocketWithUser) =&gt; {
    if (socket.userId) {
      socket.join(socket.userId);
  
      socket.emit("joined", `user ${socket.userId} joined`);
    }
  
    socket.on("disconnect", () =&gt; {
      console.log("disconnect");
    });
  });</code></pre>
  </div>
  </div>
  </p>
  <p>and in another route sockethandler</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import { Request } from "express";
  import { NotificationProps } from "types/notification";
  
  export const sendNotification = (
    req: Request,
    notification: NotificationProps
  ) =&gt; {
    const io = req.app.get("socketio");
  
    io.sockets
      .in(String(`${notification.receiver}`))
      .emit("newNotification", notification);
  };</code></pre>
  </div>
  </div>
  </p>
  <p>the like post route looks like</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>export const likePost = async (req: RequestWithUser, res: Response) =&gt; {
    const { postId } = req.body;
  
    const post = await PostModel.findById(postId);
  
    if (!post) return res.status(400).send({ msg: `post does not exist` });
  
    const checkIfLiked = post.likes.find(
      (item: any) =&gt; String(item.user._id) === String(req.user_id)
    );
  
    if (!checkIfLiked) {
      await post.updateOne(
        {
          $push: { likes: { user: req.user_id } },
        },
        { new: true }
      );
  
      const notification = new Notification({
        sender: req.user_id,
        receiver: post.user,
        notificaitonType: "like",
      });
  
      await notification.save();
  
      sendNotification(req, notification);
  
      return res.status(200).send({ success: true });
    }
  
    const postWithOutLike = await post.updateOne(
      {
        $pull: { likes: { user: req.user_id } },
      },
      { new: true }
    );
  
    return res.status(200).send({ postWithOutLike });
  };</code></pre>
  </div>
  </div>
  </p>
  <p>in the frontend react app just calling it like:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  socketIo().on("newNotification", (data) =&gt; {
      console.log({ data });
    });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>any help please?</p>