0.1.0-rc.0 • Published 2 years ago

@torolocos/react-rtc v0.1.0-rc.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

React-RTC

This library provides you simple and versatile wrapper around WebRTC technology in React ecosystem.

Installation

Yarn

yarn add @torolocos/react-rtc

npm

npm install @torolocos/react-rtc

Usage

  1. Run signaling server.

  2. Wrap your top level component with RtcProvider.

import { RtcProvider } from '@torolocos/react-rtc';
import Chat from './components/Chat';

const App = () => {
  return (
    <RtcProvider
      signalingServer="ws://localhost:8001/"
      iceServers={[{ urls: 'stun:stun.l.google.com:19302' }]}
    >
      <Chat />
    </RtcProvider>
  );
};

export default App;
  1. Use useRtc hook in your components.
import { useEffect, useState, useRef } from 'react';
import { type RtcEvent, Message, useRtc } from '@torolocos/react-rtc';
import './styles.css';

type MessageMetadata = { username?: string };

const isMessage = (message: unknown): message is Message<MessageMetadata> => {
  return (
    message instanceof Message &&
    'username' in message.metadata &&
    typeof message.metadata.username === 'string'
  );
};

const Chat = () => {
  const { send, enter, leave, on, off } = useRtc();
  const [inputValue, setInputValue] = useState('');
  const [messageData, setMessageData] = useState<Message<MessageMetadata>[]>(
    []
  );
  const [error, setError] = useState('');
  const username = useRef(Math.random().toPrecision(4).toString());

  const onMessageSend = () => {
    if (send) send<MessageMetadata>(inputValue, { username: username.current });

    setInputValue('');
  };

  const handleMessageReceived = (event: RtcEvent<'receive'>) => {
    if (isMessage(event.detail)) {
      const message = event.detail;

      setMessageData((messages) => [...messages, message]);
    }
  };

  const handleMessageSent = (event: RtcEvent<'send'>) => {
    if (isMessage(event.detail)) {
      const message = event.detail;

      setMessageData((messages) => [...messages, message]);
    }
  };

  const handleError = () => setError('Err');

  useEffect(() => {
    if (enter) enter();
    if (on) {
      on('receive', handleMessageReceived);
      on('send', handleMessageSent);
      on('error', handleError);
    }

    return () => {
      if (off) {
        off('receive', handleMessageReceived);
        off('send', handleMessageSent);
        off('error', handleError);
      }
      if (leave) leave();
    };
  }, []);

  return (
    <>
      {error && <div className="errorText">Something went wrong</div>}
      <div>
        {messageData.map(({ id, message, metadata }) => (
          <div key={id}>
            {metadata?.username}: {message}
          </div>
        ))}
      </div>
      <input
        value={inputValue}
        onChange={({ target: { value } }) => setInputValue(value)}
      />
      <button onClick={onMessageSend}>send</button>
    </>
  );
};

export default Chat;

See the example app.

0.1.0-rc.3

2 years ago

0.1.0-rc.2

2 years ago

0.1.0-rc.1

2 years ago

1.2.0

2 years ago

0.4.9

2 years ago

0.4.8

2 years ago

0.4.22-next.0

2 years ago

1.0.0-0

2 years ago

0.3.0

2 years ago

1.1.1-alpha.0

2 years ago

0.4.20

2 years ago

1.1.0

2 years ago

0.4.21

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

0.1.0-rc.0

2 years ago

0.4.19

2 years ago

1.1.10

2 years ago

0.4.10

2 years ago

0.4.17

2 years ago

0.4.18

2 years ago

0.4.15

2 years ago

0.4.16

2 years ago

0.4.13

2 years ago

0.4.14

2 years ago

0.4.11

2 years ago

0.4.12

2 years ago

0.2.0

2 years ago

0.4.5

2 years ago

0.4.4

2 years ago

0.4.6

2 years ago

0.4.0

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago