2

I'm using Node.js with Rails for a basic chat app. Now when i refreshed the page all messages get deleted. So i want to store messages in rails database using AJAX. How can i do this?

nodejs/index.js

console.log("Server is starting...");

const http = require('http');

const hostname = '0.0.0.0';
const port = '8000';

console.log("Creating server...");

const server = http.createServer().listen(port, hostname);

console.log('chat server running on '+ hostname + ':' + port);

let socketList = require('socket.io').listen(server);

socketList.sockets.on('connection', function (socket) {
    console.log('connection received');
    console.log('socket in node = ', socket);

    socket.on('disconnect', function () {
        console.log('socket disconnected');
    });

    socket.on('join_room', function (data) {
        socket.join(data.chatroom);

        socketList.in(data.chatroom).emit('user_joined', {
           user_email: data.user_email,
           chatroom: data.chatroom
        });   
    });

    socket.on('send_message', function (data) {
        console.log(data.message, data.user_email);
        socketList.in(data.chatroom).emit('receive_message', {
            message: data.message,
            user_email: data.user_email
        });
    });
});

rails/application.js

window.addEventListener('load', function (ev) {

    var user_email = $('#current_user_email').html(); // checking if user login

    if(user_email){
        var socket = io.connect('http://0.0.0.0:8000');
        console.log('socket in rails = ' , socket);

        socket.on('connect', function () {
            console.log('connection established to node server');
        });

        socket.emit('join_room',
        {
            user_email: user_email,
            chatroom: 'home'
        });

        socket.on('user_joined', function (data) {
            console.log(data.user_email + ' joined ' + data.chatroom);
        });

        $('#send-message').click(function () {
            let msg = $('#chat-message-input').val();

            if(msg !==''){
                socket.emit('send_message', {
                    message: msg,
                    user_email: user_email,
                    chatroom: 'home'
                });
            }

            $('#chat-message-input').val('');
        });

        socket.on('receive_message', function (data) {
            console.log(data.user_email, data.message);

            let newMessage = $('<li>');
            let messageType = 'other-message';
            if (data.user_email === user_email){
                messageType = 'self-message';
            }
            newMessage.addClass(messageType);

            newMessage.append($('<span>', {
                'html': data.message
            }));
            newMessage.append($('<sub>', {
                'html': data.user_email
            }));

            $('#chat-messages-list').append(newMessage);
        });

Also what are the different methods can be used to store messages in database? Can we also store directly in the Rails database just using Node.js?

Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
Vishal Goel
  • 170
  • 1
  • 1
  • 10

1 Answers1

1

Not gonna write code but basically u can start:

rails g scaffold Chat name:text chat:text

then post with ajax to /chat.json set data like {name:user_name, chat:texxt}

Then also u need to ask all user chats from /chat.json Also u need to write in rails in index function

if params[:id]) do
  @chat = Chat.find(name:username)
else 
  @chat = Chat.all
end

Then show those messages to user but this is basic concept to get u going

Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
Imre Raudsepp
  • 1,068
  • 8
  • 8
  • Sorry.. not understand what are you trying to convey here? I'm getting all information about user and messages in application.js . So how can i use that information outside of application.js to store chats – Vishal Goel Apr 19 '18 at 20:29
  • inside application.js u send ajax calls with data to rails route where u save the data and anothe route where u can retive data – Imre Raudsepp Apr 19 '18 at 20:46