
์ฑ์์ ์ฑํ
๊ธฐ๋ฅ์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ๋ค์ํ๋ค.
React Native์์๋ ์ค์๊ฐ ๋ฐ์ดํฐ ์ก์์ ์ด ๊ฐ๋ฅํด์ผ ํ๋ฉฐ, ๋คํธ์ํฌ ์ํ๊ฐ ๋ถ์์ ํด๋ ์์ ์ ์ธ ๋ฉ์์ง ์ฒ๋ฆฌ๊ฐ ํ์ํ๋ค.
์ด๋ฒ ๊ธ์์๋ ์ค์๊ฐ ์ฑํ ๊ตฌํ ์ ๋ต์ ์ ๋ฆฌํ๊ณ , ๊ฐ ๊ธฐ์ ๋ณ ์ฅ๋จ์ ์ ๋น๊ตํ๋ค.
WebSocket์ ์๋ฒ์ ํด๋ผ์ด์ธํธ ๊ฐ ์๋ฐฉํฅ ํต์ ์ ์ง์ํ๋ค.
React Native์์๋ react-native-websocket ๋๋ Socket.io-client๋ฅผ ๋ง์ด ์ฌ์ฉํ๋ค.
npm install socket.io-client
// chatSocket.ts
import { io } from 'socket.io-client';
export const socket = io('https://your-chat-server.com', {
transports: ['websocket'],
});
// ChatScreen.tsx
import { useEffect, useState } from 'react';
import { View, Text, TextInput, Button } from 'react-native';
import { socket } from './chatSocket';
export default function ChatScreen() {
const [messages, setMessages] = useState<string[]>([]);
const [input, setInput] = useState('');
useEffect(() => {
socket.on('receiveMessage', (msg) => {
setMessages((prev) => [...prev, msg]);
});
return () => {
socket.off('receiveMessage');
};
}, []);
const sendMessage = () => {
socket.emit('sendMessage', input);
setInput('');
};
return (
<View>
{messages.map((m, i) => (
<Text key={i}>{m}</Text>
))}
<TextInput value={input} onChangeText={setInput} />
<Button title="์ ์ก" onPress={sendMessage} />
</View>
);
}
Firebase๋ ๋ณ๋์ ์๋ฒ ๊ตฌ์ถ ์์ด ์ค์๊ฐ ๋ฐ์ดํฐ ๋๊ธฐํ๋ฅผ ์ ๊ณตํ๋ค.
์ฅ์ ์ ๋น ๋ฅธ ๊ฐ๋ฐ๊ณผ ์์ ์ฑ, ๋จ์ ์ ๋๊ท๋ชจ ํธ๋ํฝ ์ ๋น์ฉ ์ฆ๊ฐ๋ค.
npm install @react-native-firebase/app @react-native-firebase/firestore
// chatService.ts
import firestore from '@react-native-firebase/firestore';
export function sendMessage(roomId: string, message: string) {
return firestore().collection('rooms').doc(roomId).collection('messages').add({
text: message,
createdAt: new Date(),
});
}
export function subscribeMessages(roomId: string, callback: (msgs: any[]) => void) {
return firestore()
.collection('rooms')
.doc(roomId)
.collection('messages')
.orderBy('createdAt', 'asc')
.onSnapshot((snapshot) => {
callback(snapshot.docs.map((doc) => doc.data()));
});
}
| ๊ธฐ์ | ์ฅ์ | ๋จ์ | ์ถ์ฒ ์ฌ์ฉ ์ฌ๋ก |
|---|---|---|---|
| WebSocket / Socket.io | ๋น ๋ฅธ ์๋ต, ์์ ๋ก์ด ๊ตฌํ | ์๋ฒ ๊ตฌ์ถ ํ์ | ๋๊ท๋ชจ ์ค์๊ฐ ์ฑํ |
| Firebase | ๋น ๋ฅธ ๊ฐ๋ฐ, ์๋ฒ ๋ถํ์ | ๋๊ท๋ชจ ์ ๋น์ฉ ๋ถ๋ด | ์คํํธ์ MVP, ์๊ท๋ชจ ์ฑํ |
| Pusher / Ably | ์๋ฒ๋ฆฌ์ค, ์์ ์ฑ | ์ฌ์ฉ๋ ์ ํ, ๋น์ฉ | ์ด๋ฒคํธ ๊ธฐ๋ฐ ์ค์๊ฐ ๊ธฐ๋ฅ |
๐ฌ "์ค์๊ฐ ์ฑํ ์ ๊ธฐ์ ๋ณด๋ค ์ด์ ์ ๋ต์ด ๋ ์ค์ํ๋ค."