3

I want to implement a simple notification system. When user1 likes user2's post, user2 should get a real-time notification from user1.

Here is a part of the client function (Redux action) where someone likes a post:

.then(() => {
  const socket = require("socket.io-client")(
    "http://localhost:5000"
  );
  socket.emit("like", user, post);
});

Here is the server socket function where a notification is created after user1 likes user2's post:

io.on("connection", (socket) => {
    socket.on("like", async (user, post) => {
        if (!post.post.likedBy.includes(user.user._id)) {
            const Notification = require("./models/Notification");

            let newNotification = new Notification({
                notification: `${user.user.username} liked your post!`,
                sender: user.user._id,
                recipient: post.post.by._id,
                read: false,
            });

            await newNotification.save();

            io.emit("notification");
        }
    });
});

Here is the client function after the notification is created:

socket.on("notification", () => {
        console.log("liked");
});

Now the problem with this is the console.log('liked') appears for both user1 and user2. How can I emit to only that user that receives the notification? How can socket.io find this specific user2 that receives the notification from user1?

4
  • Not only user1 and user2 but all users will receive the notification here
    – Namysh
    Commented Jul 29, 2020 at 11:08
  • Yes, that's what I'm trying to solve. I only want the recipient to receive a notification.
    – David
    Commented Jul 29, 2020 at 11:43
  • Does my below answer works ?
    – Namysh
    Commented Jul 29, 2020 at 11:45
  • I will try later today and get back to you, thanks! :)
    – David
    Commented Jul 29, 2020 at 11:47

1 Answer 1

5

You should store a list (array or object) of all users like this :

(note that the list has to be updated when a user connects or leaves the socket server)

// an example of structure in order to store the users
const users = [
  {
    id: 1,
    socket: socket
  },
  // ...
];

And then you can target the post owner and send him a notification like this :

// assuming the the 'post' object contains the id of the owner
const user = users.find(user => user.id == post.user.id);
// (or depending of the storage structure) 
// const user = users[post.user.id]
user.socket.emit('notification');

Here an example :

const withObject = {};
const withArray = [];

io.on('connection', socket => {
  const user = { socket : socket };
  socket.on('data', id => {
    // here you do as you want, if you want to store just their socket or another data, in this example I store their id and socket
    user.id = id;
    withObject[id] = user;
    withArray[id] = user;
    // or withArray.push(user);
  });

  socket.on('disconnect', () => {
    delete withObject[user.id];
    delete withArray[user.id];
    // or let index = users.indexOf(user);
    // if(index !=== -1) users.splice(index, 1);

  });
});

There is plenty way of achieving what i'm trying to explain but the main idea is to link the socket with an index (user id for example) in other to retrieve it later in the code.

12
  • All the users are in the MongoDB database. How can I store each of them in the socket?
    – David
    Commented Jul 29, 2020 at 14:18
  • You can store their socket in a variable on the server. When a user connects to the socket server, you add him as an object (his id and his socket), in a global array. When he disconnects, you remove him from the global array.
    – Namysh
    Commented Jul 29, 2020 at 14:21
  • Could you please help with adding them to the socket server? I see there is an array but how do they get added to it automatically?
    – David
    Commented Jul 29, 2020 at 14:27
  • On 'connection' event you can save the socket in an array (or object). In order the store the id you have to emit another event like a 'data' event. I add the code in my answer
    – Namysh
    Commented Jul 29, 2020 at 14:33
  • Thank you for your patience. So do I need to emit to the recipient's socket.id? Nothing happens with user.socket.emit('notification');
    – David
    Commented Jul 29, 2020 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.