TheTechGuy
TheTechGuy

Reputation: 1608

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
    })
    })

Upvotes: 8

Views: 12727

Answers (4)

TheTechGuy
TheTechGuy

Reputation: 1608

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);

        })
    })
})

Upvotes: 4

Pourya8366
Pourya8366

Reputation: 3684

you need to store the messages in a database and when a new user entered the chat, read the old messages from the db and show them to user, i don't think there is another solution for that!

Update: so you could just make an array and store every message in the form of an js object with the sender info and when a user entered, once in the connection event send the array to client and in the client parse the array in the way you want.

for your question about how long should get, as i said you can send the prev messages to the client only once and in the first entrance.

Update 2: Send History:

you can use the acknowledgements part of socket.io:

 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, ack) {
            io.sockets.emit("r_name", "<strong>" + data + "</strong> Has Joined The Chat");
            console.log(data)

            //**** Make change Here
            ack({ history });

        });
        socket.on("Send_msg", function (data) {

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


            //Now Listening To A Chat Message
        });
    });

And to get the ack data in client part:

socket.emit("Register_Name", your_data, function (ack) {
        //you get the prev messages
        console.log(ack);
    });

and you don't need to store the clients for this!

Upvotes: 0

TheBeardedOne
TheBeardedOne

Reputation: 292

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.

Upvotes: 8

deepak thomas
deepak thomas

Reputation: 1180

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.

Upvotes: 1

Related Questions