L
L
Lelya6662019-11-10 19:52:00
Socket.io
Lelya666, 2019-11-10 19:52:00

Why are socket.io messages only displayed in one tab?

Chat with react-redux, redux-thunk, socket.io. Messages come to the console from both tabs, but are not displayed from each other. Please help me figure out what I wrote wrong.
server

const app = require('express')()
const http = require('http').Server(app)
const io = require('socket.io')(http)

const port = 3001

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/public/index.html')
})

io.on('connection', client => {
    let addedUser = false
    
    client.on('new user', username => {
        if (addedUser) return

        client.username = username
        addedUser = true
        console.log(`${client.username} joined`)
        client.broadcast.emit('user join', { username: client.username })
    })
    // message
    client.on('new message', data => {
        console.log('MSG: ', data)
        io.emit('new message', { message: data })
    })

    client.on('disconnect', () => {
        console.log(`${client.username} has disconnected`)
    })
})

http.listen(port, () => {
    console.log(`listening on *:${port}`)
})

api/socket.io
import io from 'socket.io-client'


const socket = io('http://localhost:3001')

export const messagesAPI = {
    addNewMessage(newMessageText) {
        socket.emit('new message', newMessageText)

    },
socketMessage(data) {
    socket.on('new message', data)
},
    newUserSocket(name) {
        socket.emit('new user', name)
    }

}



export default socket;

reducer, actions and redux-thunk
import { messagesAPI} from "../api";

const SEND_MESSAGE = "SEND_MESSAGE";
const USERS = "USERS"

let initialState = {
    messages: [],
    users: []

}

const messagesReducer = (state = initialState, action) => {
    switch (action.type) {
        case SEND_MESSAGE: {
            let newMessage = action.newMessageText;
            return {
                ...state,
                messages: [...state.messages, {id: 2, message: newMessage}],
            }
        }
        case USERS: {
            let newUser = action.newUserJoined;
            return {
                ...state,
                users: [...state.users,{id: 1, name: newUser }]
            }
        }
        default:
            return state;
    }

}
export const setMessage = (newMessageText) => ({type: SEND_MESSAGE, newMessageText})
export const newUser = (newUserJoined) => ({type: USERS, newUserJoined})

export const messageTC = ( newMessageText) => (dispatch) => {
    messagesAPI.addNewMessage(newMessageText)
    dispatch(setMessage(newMessageText))
}

export  const newUserSocket = (newUserJoined, name) => (dispatch) => {
    messagesAPI.newUserSocket(name)
    dispatch(newUser(newUserJoined))
}

export const socketAdd = (data, newMessageText) => (dispatch) => {
    messagesAPI.socketMessage(data, newMessageText)
    dispatch(setMessage(newMessageText))
}


export default messagesReducer;

container
import React from 'react'
import {connect} from "react-redux";
import Dialogs from "./Dialogs";
import {compose} from "redux";

import {messageTC, socketAdd, newUserSocket} from "../../Redux/reducers/messages-reducer";

class DialogsContainer extends React.Component {

    componentDidMount = () => {
        this.props.userNew(this.props.name)
        this.props.socketAdd(this.props.data)
    }
    addNewMessage = (newMessageText) => {
        this.props.messageTC(newMessageText)

    }
    render() {

        return (
            <Dialogs messages={this.props.messages}
                     addNewMessage={this.props.messageTC}/>
        )
    }
}
let mapStateToProps = (state) => {
    return {
        messages: state.dialogsPage.messages,
        newMessageText: state.dialogsPage.newMessageText,
        messageTC: state.dialogsPage.messageTC
    }
}

export default compose(connect(mapStateToProps, {messageTC, userNew: newUserSocket, socketAdd}))(DialogsContainer)

5dc8408f6d49a018078577.jpeg5dc8409cc3d67013077007.jpeg

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question