J
J
juventino962018-04-01 16:13:36
MongoDB
juventino96, 2018-04-01 16:13:36

Problem with socket.io?

When I write a message in the chat, it is added and socket io works, but only in the browser from which they wrote in other places it is not updated.

socket.on('connect', function () {
        $('body').on('click', '.chat-app-messenger-editor button.btn', function (e) {
            e.preventDefault();

            $('.chat-app-loading').css('display', 'block');

            socket.emit('add new message', {
                roomId: $('.chat-app').attr('data-room-id'),
                message: $('#chat-app-messenger-message').val()
            });

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

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

            $('.chat-app-loading').css('display', 'none');

            $('.chat-app-messenger ul').append(data);
        });
    });

io.on('connection', function(socket) {
        socket.on('add new message', function (data) {
            if (! data.message || 0 === data.message.length) {
                socket.emit('new message', 'Message can\'t be blank');
            } else {
                ChatMessages.create({
                    room_id: data.roomId,
                    message: data.message,
                    username: socket.request.session.user.username
                }, function(error, newMessage){
                    if(error) throw error;

                    ChatMessages.findOne({_id: new ObjectID(newMessage._id), room_id: new ObjectID(data.roomId)}, function (error, message) {
                        if(error) throw error;

                        var fileHtml = fs.readFileSync(process.cwd() + '/app/views/partials/chat-message.ejs', 'utf8');
                        var html = ejs.render(fileHtml, {
                            message: message,
                            timeago: timeago
                        });

                        socket.emit('new message', html);
                    });
                });
            }
        });
    });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2018-04-01
@vshvydky

1. chats are divided into rooms, you implement the protocol, you can type irts. For example, there is a request:join subscription on the server.
When your client decides to enter a certain chat, it sends a request to the server emit('request:join', roomName); there is a reaction on the server, processing whether the client can join this chat or not.
2. in the event processing on the server, you have on('request.join', roomName=>socket.join(roomName); At this moment, you need to iterate over all active sockets, to see if the second window is used by the client (if required certainly).
3. According to this principle, you can build both general chats and private ones. People begin to communicate not with each other, but write to the channel. the client writes emit('message:send', {room: id, message}), on the server it is sent to the room as .on('message:send', message=> io.to(message.room).emit(message .message)
As a result, it turns out that the clients communicate with each other, but in fact they only send all the data to their server, and it already decides whether to notify the others about it or not.

V
Vladimir Skibin, 2018-04-01
@megafax

I join Vitaly 's more extensive answer . In your case, you send a message only to a specific socket, and not to all those who are interested.
io.emit('new message', html);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question