Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send new user the old sent messages with socket.io

I am using this tutorial. I need to send old messages to the new connected user. How can I do that? I mean the new users should be able to see the old messages.

for my application, i don't need to store the data in a database. Once the server is turned off then the data should be removed. but how long the server runs on each session the clients should get previous message

I have tried

 const history = []
//Telling Express+Socket.io App To Listen To Port
io.sockets.on("connection",function(socket){

    socket.emit("Start_Chat");
    //On Event Registar_Name
    socket.on("Register_Name",function(data){
            console.log(data)
       io.sockets.emit("r_name","<strong>"+data+"</strong> Has Joined The Chat");
       //Now Listening To A Chat Message
       socket.on("Send_msg",function(data){
               history.push(data)
               console.log(history)
       io.sockets.emit("msg",data);
       //Now Listening To A Chat Message
    })
    })
})

but how can I say when a new client enters send history data?

UPDATE: I have tried the following but it is always sending the history

 const history = []
 const client = []
//Telling Express+Socket.io App To Listen To Port
io.sockets.on("connection",function(socket){

        client.push({id : socket.client.id})
        console.log(client)

    socket.emit("Start_Chat");
    //On Event Registar_Name
    socket.on("Register_Name",function(data){
            console.log(data)
       io.sockets.emit("r_name","<strong>"+data+"</strong> Has Joined The Chat");
       //Now Listening To A Chat Message
       socket.on("Send_msg",function(data){

               history.push(data)
               console.log(history)
               if(client.find(e => e.id !== socket.client.id)) {
                io.sockets.emit("msg",history);
               } else {
                io.sockets.emit("msg",data);
               }

       //Now Listening To A Chat Message
    })
    })
like image 768
TheTechGuy Avatar asked Feb 01 '18 11:02

TheTechGuy


People also ask

How do I send a message to a socket from a user?

When a user connects, it should send a message to the server with a username which has to be unique, like an email. A pair of username and socket should be stored in an object like this: var users = { '[email protected]': [socket object], '[email protected]': [socket object], '[email protected]': [socket object] }

Is it possible to retrieve old messages from a socket?

I wouldn't suggest using socket for retrieving old messages. What you can do is that, you can use AJAX to retrieve old messages and Socket for the live chat. Is there any advantage of using a separate AJAX call?

What is socket Io in Node JS?

Users are connected with a Node JS server using a client-side library called Socket IO. Users can also join the room which will be helpful if you are creating a group chat app. There are 4 ways in which socket events are fired. Send event to all connected users, including the sender. Send event to all users, except the sender.

What is socket ID and receiver name?

whenever a user joined to the server, socket details will be generated including ID. This is the ID really helps to send a message to particular people. here name is the receiver name. Example: So, now we can get that socket.id with the receiver name whenever we are sending message: for this we need to know the receivername.


3 Answers

There are several ways depending on how complex you want to make your chat application.

Simple solution: You create some type of data structure (possibly an array) which holds the last n messages. Whenever a new message is added, you remove the first one that was added. When the onConnect event is triggered, you send this entire data structure to the client. This is more for a proof of concept solution which you can later change.

Pros: Easy to implement

Cons: All the data on the server is essentially volatile, if you were to restart the application, all data would be lost. Additionally it would not scale across multiple server instances.

A little more complex solution: Store all messages in a database and query the database to retrieve the last n messages when a user connects.

Pros: All data is persistent and if the server were to crash/be stopped, you still would be able to retrieve the data.

Cons: You need to maintain the database as messages will take up more space over time. To remedy this you can always delete all messages older than x days automatically.

like image 158
TheBeardedOne Avatar answered Oct 22 '22 16:10

TheBeardedOne


Finally, Figured it out

    const history = []
 const client = []
//Telling Express+Socket.io App To Listen To Port
io.sockets.on("connection",function(socket){

        client.push({id : socket.client.id})
        console.log(client)

        var getClientID = client.find(e => (e.id === socket.client.id))
       console.log("the Client", getClientID)
       if(getClientID){
        //io.sockets.emit("msg",history);
        socket.emit("msg",history);

       }

    socket.emit("Start_Chat");
    //On Event Registar_Name
    socket.on("Register_Name",function(data){
            console.log(data)
       io.sockets.emit("r_name","<strong>"+data+"</strong> Has Joined The Chat");

       //Now Listening To A Chat Message
       socket.on("Send_msg",function(data){

               history.push(data)
               console.log(history)
               io.sockets.emit("msg",data);

        })
    })
})
like image 23
TheTechGuy Avatar answered Oct 22 '22 16:10

TheTechGuy


I wouldn't suggest using socket for retrieving old messages. What you can do is that, you can use AJAX to retrieve old messages and Socket for the live chat.

like image 1
deepak thomas Avatar answered Oct 22 '22 18:10

deepak thomas