Text Chat
This guide shows how to implement text chat in your application using data channels. Data channels allow you to send and receive arbitrary binary data between peers in a room.
Prerequisites
Before implementing text chat, you must be connected to a room. Data channels only work after you have successfully joined a room.
For a deeper understanding of how data channels work, including channel types and broadcast behavior, see Data Channels.
Step 1 — Set up the chat hook
Use the useDataChannel hook to work with data channels. The typical flow is:
- Initialize the data channel after connecting to a room
- Subscribe to incoming messages
- Publish messages to other peers
- React (Web)
- React Native (Mobile)
import {useConnection ,useDataChannel } from "@fishjam-cloud/react-client"; import {useCallback ,useEffect ,useState } from "react"; export functionuseChat () { const {peerStatus } =useConnection (); const {initializeDataChannel ,publishData ,subscribeData ,dataChannelReady , } =useDataChannel (); const [messages ,setMessages ] =useState <string[]>([]); // Step 1: Initialize data channel when connecteduseEffect (() => { if (peerStatus === "connected") {initializeDataChannel (); } }, [peerStatus ,initializeDataChannel ]); // Step 2: Subscribe to incoming messagesuseEffect (() => { if (!dataChannelReady ) return; constunsubscribe =subscribeData ( (data :Uint8Array ) => { constmessage = newTextDecoder ().decode (data );setMessages ((prev ) => [...prev ,message ]); }, {reliable : true }, ); returnunsubscribe ; }, [subscribeData ,dataChannelReady ]); // Step 3: Publish messages constsendMessage =useCallback ( (text : string) => { if (!dataChannelReady ) return; constencoded = newTextEncoder ().encode (text );publishData (encoded , {reliable : true }); }, [publishData ,dataChannelReady ], ); return {messages ,sendMessage ,ready :dataChannelReady }; }
Mobile SDK support for data channels will be available in a future release.
Step 2 — Use the chat hook in your component
Once you have the hook, you can use it in any component to send and display messages:
- React (Web)
- React Native (Mobile)
functionChatPanel () { const {messages ,sendMessage ,ready } =useChat (); const [input ,setInput ] =useState (""); consthandleSend = () => { if (input .trim ()) {sendMessage (input );setInput (""); } }; if (!ready ) { return <div >Connecting to chat...</div >; } return ( <div > <div > {messages .map ((msg : string,i : number) => ( <div key ={i }>{msg }</div > ))} </div > <input value ={input }onChange ={(e ) =>setInput (e .target .value )}onKeyDown ={(e ) =>e .key === "Enter" &&handleSend ()} /> <button onClick ={handleSend }>Send</button > </div > ); }
Mobile SDK support for data channels will be available in a future release.
Why use the reliable channel?
For chat messages, we use { reliable: true } because:
- Ordered delivery: Messages arrive in the order they were sent
- Guaranteed delivery: All messages will be delivered, with automatic retransmission if needed
This ensures no chat messages are lost or arrive out of order.
See also
- Data Channels — detailed explanation of channel types and broadcast behavior
- Connecting to a room — prerequisite for using data channels
useDataChannelAPI reference