WebSocket

Back

Loading concept...

๐Ÿ”Œ WebSocket in React Native

Real-Time Communication Made Simple


๐ŸŒ‰ The Magic Bridge Analogy

Imagine you have a walkie-talkie with your best friend. Once you turn it on and connect, you can both talk and listen at the same time, without hanging up and calling again.

Thatโ€™s exactly what a WebSocket is! Itโ€™s like a walkie-talkie for your app and a server.

Regular HTTP (like a phone call):

  • You call โ†’ Server answers โ†’ You hang up
  • Need to talk again? Call again! ๐Ÿ“ž

WebSocket (like a walkie-talkie):

  • Connect once โ†’ Talk anytime โ†’ Listen anytime ๐Ÿ“ป
  • No need to reconnect!

๐Ÿ“ก What is WebSocket?

WebSocket is a communication protocol that creates a persistent, two-way connection between your React Native app and a server.

Why is this amazing?

Feature HTTP (Old Way) WebSocket (Cool Way)
Connection Open โ†’ Close โ†’ Open Stay Open Forever
Who Talks First? Only Client Both!
Speed Slower Super Fast
Real-time? Nope Yes!

Real Examples:

  • ๐Ÿ’ฌ Chat apps (WhatsApp, Messenger)
  • ๐ŸŽฎ Online games
  • ๐Ÿ“ˆ Stock price updates
  • ๐Ÿ”” Live notifications

๐Ÿ”— WebSocket Connection

How to Create Your Walkie-Talkie

Think of connecting a WebSocket like this:

  1. ๐Ÿ“ž Dial the number (create connection)
  2. ๐Ÿ“ณ Wait for pickup (connection opens)
  3. ๐Ÿ—ฃ๏ธ Start talking (send messages)
  4. ๐Ÿ‘‚ Listen (receive messages)
  5. ๐Ÿ“ด Hang up when done (close connection)

๐Ÿ› ๏ธ Creating a WebSocket Connection

// Create your walkie-talkie!
const ws = new WebSocket('wss://example.com/socket');

// When connection opens (friend picked up!)
ws.onopen = () => {
  console.log('Connected! ๐ŸŽ‰');
};

// When you receive a message
ws.onmessage = (event) => {
  console.log('Got message:', event.data);
};

Breaking It Down:

new WebSocket(url) - Creates the connection

  • ws:// = Not secure (like shouting in public)
  • wss:// = Secure (like whispering privately) โœ…

onopen - Runs when connected successfully

onmessage - Runs when server sends you something


๐Ÿ“ค Sending Messages

// Make sure connection is open first!
if (ws.readyState === WebSocket.OPEN) {
  ws.send('Hello Server!');

  // Send JSON data (most common)
  ws.send(JSON.stringify({
    type: 'chat',
    message: 'Hi there!'
  }));
}

The readyState Values:

Value Meaning Analogy
0 CONNECTING Phone is ringingโ€ฆ
1 OPEN Friend answered! โœ…
2 CLOSING Saying goodbyeโ€ฆ
3 CLOSED Hung up

๐Ÿšจ Handling Errors & Closing

// When something goes wrong
ws.onerror = (error) => {
  console.log('Oops! Error:', error);
};

// When connection closes
ws.onclose = (event) => {
  console.log('Connection closed');
  console.log('Code:', event.code);
  console.log('Reason:', event.reason);
};

// To close the connection yourself
ws.close();

๐Ÿ”„ Complete Connection Example

import { useEffect, useState } from 'react';

function ChatComponent() {
  const [socket, setSocket] = useState(null);
  const [messages, setMessages] = useState([]);

  useEffect(() => {
    // Create connection
    const ws = new WebSocket('wss://chat.example.com');

    ws.onopen = () => {
      console.log('Connected!');
    };

    ws.onmessage = (event) => {
      const data = JSON.parse(event.data);
      setMessages(prev => [...prev, data]);
    };

    ws.onerror = (error) => {
      console.error('WebSocket error:', error);
    };

    setSocket(ws);

    // Cleanup when component unmounts
    return () => ws.close();
  }, []);

  return (/* your UI */);
}

๐ŸŽจ WebSocket Patterns

Smart Ways to Use WebSockets

Now that you know how to connect, letโ€™s learn the patterns - the smart ways to use WebSockets in real apps!


๐Ÿ”„ Pattern 1: Reconnection Pattern

The Problem: What if your internet drops? Your walkie-talkie disconnects!

The Solution: Auto-reconnect! Keep trying to connect again.

graph TD A["Connected"] -->|Internet drops| B["Disconnected"] B --> C{Wait 2 seconds} C --> D["Try to Reconnect"] D -->|Success| A D -->|Fail| C

Code Example:

function createWebSocket() {
  const ws = new WebSocket('wss://example.com');

  ws.onclose = () => {
    console.log('Disconnected! Reconnecting...');
    // Wait 3 seconds, then try again
    setTimeout(createWebSocket, 3000);
  };

  return ws;
}

const socket = createWebSocket();

Better Version with Limits:

let retryCount = 0;
const MAX_RETRIES = 5;

function connectWithRetry() {
  if (retryCount >= MAX_RETRIES) {
    console.log('Too many failures. Giving up.');
    return;
  }

  const ws = new WebSocket('wss://example.com');

  ws.onopen = () => {
    retryCount = 0; // Reset on success!
  };

  ws.onclose = () => {
    retryCount++;
    const delay = Math.min(1000 * retryCount, 10000);
    setTimeout(connectWithRetry, delay);
  };
}

๐Ÿ’“ Pattern 2: Heartbeat Pattern

The Problem: How do you know if the connection is still alive? Sometimes it justโ€ฆ dies silently.

The Solution: Send a โ€œpingโ€ every few seconds. Like saying โ€œAre you still there?โ€

graph LR A["App"] -->|Ping every 30s| B["Server"] B -->|Pong| A A -->|No Pong?| C["Reconnect!"]

Code Example:

let heartbeatInterval;

function startHeartbeat(ws) {
  heartbeatInterval = setInterval(() => {
    if (ws.readyState === WebSocket.OPEN) {
      ws.send(JSON.stringify({ type: 'ping' }));
    }
  }, 30000); // Every 30 seconds
}

function stopHeartbeat() {
  clearInterval(heartbeatInterval);
}

// Usage
ws.onopen = () => startHeartbeat(ws);
ws.onclose = () => stopHeartbeat();

๐Ÿ“ฌ Pattern 3: Message Queue Pattern

The Problem: What if you try to send a message but youโ€™re not connected yet?

The Solution: Save messages in a queue, send them when connected!

const messageQueue = [];

function sendMessage(ws, message) {
  if (ws.readyState === WebSocket.OPEN) {
    ws.send(JSON.stringify(message));
  } else {
    // Save for later!
    messageQueue.push(message);
  }
}

// When connection opens, send queued messages
ws.onopen = () => {
  while (messageQueue.length > 0) {
    const msg = messageQueue.shift();
    ws.send(JSON.stringify(msg));
  }
};

๐Ÿท๏ธ Pattern 4: Message Types Pattern

The Problem: Different messages need different handling. How do you organize them?

The Solution: Use a type field and a message handler!

Message Structure:

// Every message looks like this:
{
  type: 'chat_message',    // What kind?
  payload: {               // The actual data
    text: 'Hello!',
    sender: 'Alice'
  }
}

Handler Pattern:

const handlers = {
  chat_message: (payload) => {
    addMessageToChat(payload);
  },

  user_joined: (payload) => {
    showNotification(`${payload.name} joined!`);
  },

  typing: (payload) => {
    showTypingIndicator(payload.user);
  }
};

ws.onmessage = (event) => {
  const { type, payload } = JSON.parse(event.data);

  if (handlers[type]) {
    handlers[type](payload);
  } else {
    console.warn('Unknown message type:', type);
  }
};

๐ŸŽฏ Pattern 5: Room/Channel Pattern

The Problem: In a chat app, how do you send messages to specific groups?

The Solution: Join โ€œroomsโ€ or โ€œchannelsโ€!

graph TD A["User"] -->|Join| B["Room: Gaming"] A -->|Join| C["Room: Music"] D["Other User"] -->|Join| B B -->|Message| A B -->|Message| D

Code Example:

// Join a room
function joinRoom(ws, roomId) {
  ws.send(JSON.stringify({
    type: 'join_room',
    payload: { roomId }
  }));
}

// Leave a room
function leaveRoom(ws, roomId) {
  ws.send(JSON.stringify({
    type: 'leave_room',
    payload: { roomId }
  }));
}

// Send to specific room
function sendToRoom(ws, roomId, message) {
  ws.send(JSON.stringify({
    type: 'room_message',
    payload: { roomId, message }
  }));
}

๐Ÿ”’ Pattern 6: Authentication Pattern

The Problem: How does the server know who you are?

The Solution: Send your token when connecting!

// Method 1: URL parameter
const token = 'your-auth-token';
const ws = new WebSocket(
  `wss://example.com?token=${token}`
);

// Method 2: First message after connecting
ws.onopen = () => {
  ws.send(JSON.stringify({
    type: 'authenticate',
    payload: { token: 'your-auth-token' }
  }));
};

๐Ÿงน Complete React Native Hook

Hereโ€™s everything together in a reusable hook:

import { useEffect, useRef, useState } from 'react';

export function useWebSocket(url) {
  const [isConnected, setIsConnected] = useState(false);
  const [lastMessage, setLastMessage] = useState(null);
  const wsRef = useRef(null);
  const retryCount = useRef(0);

  const connect = () => {
    const ws = new WebSocket(url);

    ws.onopen = () => {
      setIsConnected(true);
      retryCount.current = 0;
    };

    ws.onmessage = (event) => {
      setLastMessage(JSON.parse(event.data));
    };

    ws.onclose = () => {
      setIsConnected(false);
      // Auto-reconnect
      const delay = Math.min(
        1000 * Math.pow(2, retryCount.current),
        30000
      );
      retryCount.current++;
      setTimeout(connect, delay);
    };

    wsRef.current = ws;
  };

  const send = (data) => {
    if (wsRef.current?.readyState === 1) {
      wsRef.current.send(JSON.stringify(data));
    }
  };

  useEffect(() => {
    connect();
    return () => wsRef.current?.close();
  }, [url]);

  return { isConnected, lastMessage, send };
}

Usage:

function ChatScreen() {
  const { isConnected, lastMessage, send } =
    useWebSocket('wss://chat.example.com');

  return (
    <View>
      <Text>
        {isConnected ? '๐ŸŸข Online' : '๐Ÿ”ด Offline'}
      </Text>
      <Button
        title="Say Hi!"
        onPress={() => send({ text: 'Hello!' })}
      />
    </View>
  );
}

๐ŸŽ‰ Summary

You learned two big things today:

1. WebSocket Connection

  • Create with new WebSocket(url)
  • Use wss:// for security
  • Handle onopen, onmessage, onerror, onclose
  • Check readyState before sending

2. WebSocket Patterns

  • ๐Ÿ”„ Reconnection - Auto-connect when disconnected
  • ๐Ÿ’“ Heartbeat - Check if connection is alive
  • ๐Ÿ“ฌ Message Queue - Save messages for later
  • ๐Ÿท๏ธ Message Types - Organize with type handlers
  • ๐ŸŽฏ Rooms/Channels - Group communication
  • ๐Ÿ”’ Authentication - Identify users

๐Ÿ’ก Quick Tips

  1. Always use wss:// (not ws://) in production
  2. Always clean up in Reactโ€™s useEffect return
  3. Always handle errors - connections fail!
  4. Use JSON for complex data
  5. Add reconnection - networks are unreliable

Now go build something amazing with real-time features! ๐Ÿš€

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.