0.1.0-rc.0 • Published 3 years ago

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

Weekly downloads
-
License
MIT
Repository
github
Last release
3 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

3 years ago

0.1.0-rc.2

3 years ago

0.1.0-rc.1

3 years ago

1.2.0

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.22-next.0

3 years ago

1.0.0-0

3 years ago

0.3.0

3 years ago

1.1.1-alpha.0

3 years ago

0.4.20

3 years ago

1.1.0

3 years ago

0.4.21

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

0.1.0-rc.0

3 years ago

0.4.19

3 years ago

1.1.10

3 years ago

0.4.10

3 years ago

0.4.17

3 years ago

0.4.18

3 years ago

0.4.15

3 years ago

0.4.16

3 years ago

0.4.13

3 years ago

0.4.14

3 years ago

0.4.11

3 years ago

0.4.12

3 years ago

0.2.0

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.6

3 years ago

0.4.0

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago