質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

2回答

1756閲覧

React+Pusherを利用したチャット機能の問題

退会済みユーザー

退会済みユーザー

総合スコア0

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

1クリップ

投稿2020/02/12 13:48

編集2020/02/12 15:23

概要

現在、React+Laravelを利用してリアルタイムチャット機能の実装を行っています。
そこで、メッセージのPusherへの送信はできるのですが(Pusherのコンソールで確認)、PusherからのデータをReactで受け取ることができません。自分なりに色々試してみましたが、解決せず…。
どなたか解決方法のご教授お願い致します。

該当のコード

import React, { useEffect } from 'react' import { useDispatch, useSelector } from 'react-redux' import { useChannel, useEvent } from 'use-pusher' import styled from 'styled-components' import Button from '@material-ui/core/Button' import IconButton from '@material-ui/core/IconButton' import Dialog from '@material-ui/core/Dialog' import DialogActions from '@material-ui/core/DialogActions' import DialogTitle from '@material-ui/core/DialogTitle' import Divider from '@material-ui/core/Divider' import TextsmsIcon from '@material-ui/icons/Textsms' import TelegramIcon from '@material-ui/icons/Telegram' import Chat from './../../../models/Chat' import User from './../../../models/User' import Board from './../../../models/Board' import Pusher from 'pusher-js' const ChatDialog = props => { const { open, onClose, fullScreen } = props const [msg, setMsg] = React.useState('') const [messages, setMessages] = React.useState([]) const userID = useSelector(state => state.UserReducer.userID) let isChanged = true useEffect(() => { // Pusher用のキーを設定 const pusher = new Pusher(process.env.MIX_PUSHER_APP_KEY, { cluster: process.env.MIX_PUSHER_APP_CLUSTER, encrypted: true }) // チャンネルの購読 const channel = pusher.subscribe('chat') channel.bind('ChatMessageReceived', data => { console.log(data) setMessages([...messages, data]) }) }) useEffect(() => { const token = JSON.parse(User.get('token')).token const boardID = Board.getBoardID() if (token) { Chat.get(token, boardID) .then(res => { setMessages(res.data.messages) }) .catch(e => { console.log(e) }) } }, [isChanged]) const handleOnChangeMsg = e => { setMsg(e.target.value) } const handleSendMsg = () => { const token = JSON.parse(User.get('token')).token const boardID = Board.getBoardID() if (token) { Chat.send(token, boardID, msg) .then(res => { setMsg('') }) .catch(e => { console.log(e) }) } } const messageList = ( Array.from(messages).map((msg, i) => ( <React.Fragment key={i}> {msg.user_id === userID ? <UserMsg>{msg.message}</UserMsg> : <Msg>{msg.message}</Msg>} </React.Fragment> )) ) const fullDialog = ( <Dialog fullScreen open={open} scroll="paper" aria-labelledby="alert-dialog-title" aria-describedby="alert-dialog-description" > <Flex> <StyledDialogTitle><StyledTextsmsIcon /><Span>チャット</Span></StyledDialogTitle> <StyledDialogActions> <Button onClick={onClose} color="primary" autoFocus> <Span>閉じる</Span> </Button> </StyledDialogActions> </Flex> <DialogContent> <Textarea placeholder="メッセージを入力" onChange={handleOnChangeMsg} value={msg}></Textarea> <StyledIconButton onClick={handleSendMsg}><StyledTelegramIcon /></StyledIconButton> </DialogContent> <MessageArea> {messages.length === 0 ? <p>メッセージがありません</p> : messageList} </MessageArea> </Dialog > ) const nonFullDialog = ( <Dialog open={open} scroll="paper" fullWidth={true} maxWidth="sm" aria-labelledby="alert-dialog-title" aria-describedby="alert-dialog-description" > <Flex> <StyledDialogTitle><StyledTextsmsIcon /><Span>チャット</Span></StyledDialogTitle> <StyledDialogActions> <Button onClick={onClose} color="primary" autoFocus> <Span>閉じる</Span> </Button> </StyledDialogActions> </Flex> <DialogContent> <Textarea placeholder="メッセージを入力" onChange={handleOnChangeMsg} value={msg}></Textarea> <StyledIconButton onClick={handleSendMsg}><StyledTelegramIcon /></StyledIconButton> </DialogContent> <MessageArea> {messages.length === 0 ? <p>メッセージがありません</p> : messageList} </MessageArea> </Dialog > ) return ( <> {fullScreen === true ? fullDialog : nonFullDialog} </> ) } const Flex = styled.div` display: flex; justify-content: space-between; background-color: #224272; ` const Textarea = styled.textarea` resize: none; width: 100%; height: 35px; border: 1px solid #224272; ` const Span = styled.span` font-size: 0.9em; color: white; ` const StyledDialogTitle = styled(DialogTitle)` color: white; ` const DialogContent = styled.div` height: 45px; display: flex; justify-content: space-between; padding: 0; padding-left: 5px; padding-top: 5px; ` const MessageArea = styled.div` height: 450px; ` const StyledDialogActions = styled(DialogActions)` ` const StyledIconButton = styled(IconButton)` vertical-align: middle; padding: 0 10px; height: 35px; ` const StyledTextsmsIcon = styled(TextsmsIcon)` vertical-align: middle; margin-right: 3px; ` const StyledTelegramIcon = styled(TelegramIcon)` vertical-align: middle; padding: 0; ` const Msg = styled.p` width: 100% text-align: left; ` const UserMsg = styled.p` width: 100% text-align: right; ` export default ChatDialog

追記

channel.bindのなかでconsole.log(data)をしていますが、それが表示されていない為、やはりデータが取得できていないのだと思います。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

useEffectの第2引数をmessagesに変更したらいけました。ありがとうございました。

投稿2020/02/14 14:42

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

useEffectの第2引数をmessagesに変更したらいけました。

投稿2020/02/14 14:41

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問