SimpleWebRTC is a Unity-based WebRTC wrapper that facilitates peer-to-peer audio, video, and data communication over WebRTC using Unitys WebRTC package https://docs.unity3d.com/Packages/[email protected]/manual/index.html. It leverages NativeWebSocket https://github.com/endel/NativeWebSocket for signaling and supports both video and audio streaming.
- WebRTC peer-to-peer connection management
- WebSocket-based signaling
- Video and audio streaming
- Data channel communication
- Logging and debugging tools
- Usage with Photon Fusion 2
- Usage with SimpleWebRTC Web Client
A tutorial YouTube video can be found here: https://www.youtube.com/watch?v=-CwJTgt_Z3M
- Make sure, that the required dependencies are installed (
TextMeshPro
,Unity WebRTC
,NativeWebSocket
). - Go to the Unity AssetStore page: https://assetstore.unity.com/packages/slug/309727
- Install the package via Unity AssetStore.
- Got to the releases page and download the latest release.
- Make sure, that the required dependencies are installed (
TextMeshPro
,Unity WebRTC
,NativeWebSocket
). - Import the package into your Unity project.
- Create a new Unity project
- Open the Package Manager, click on the + sign in the upper left/right corner
- Select "Add package from git URL"
- Enter URL:
https://github.com/endel/NativeWebSocket.git#upm
and click in Install - After the installation finished, click on the + sign in the upper left/right corner again
- Enter URL
https://github.com/FireDragonGameStudio/SimpleWebRTC.git?path=/Assets/SimpleWebRTC
and click on Install
- Clone the repository:
git clone https://github.com/firedragongamestudio/simplewebrtc.git
- Open the Unity project in the Unity Editor.
- Ensure that the required dependencies (such as
TextMeshPro
,Unity WebRTC
andNativeWebSocket
) are installed.
The WebRTCConnection
component manages the WebRTC connection and can be attached to a GameObject in Unity.
- Install Photon Fusion 2 from Unity AssetStore -> Photon Fusion 2
- Import the Photon Fusion sample scene via Unity Package Manager.
- Use the
_Generic
scripts andPhotonSignalServer
to setup the WebRTC connection. - A tutorial/explanation YouTube video can be found here: https://www.youtube.com/watch?v=z1F_cqfdU6o
- Make sure your WebSocket signaling server is reachable, up and running.
- Checkout the SimpleWebRTC Web Client
- Run
npm install
in the web client directory, to get everything ready - Start the web client either locally (
npm run dev
ornpx vite
) or deploy it to a webspace - (Optional) Start your Unity application and make sure the WebRTC logic is up and running.
- Connect all clients to your WebSocket signaling server and wait until the signaling procedure is completed.
- Stream your video, audio and/or data to every connected client.
Property | Type | Description |
---|---|---|
IsWebSocketConnected |
bool |
Indicates whether the WebSocket connection is active. |
ConnectionToWebSocketInProgress |
bool |
Indicates whether a connection attempt is in progress. |
IsWebRTCActive |
bool |
Shows if a WebRTC session is active. |
IsVideoTransmissionActive |
bool |
Indicates whether video transmission is active. |
IsAudioTransmissionActive |
bool |
Indicates whether audio transmission is active. |
Method | Description |
---|---|
void Connect() |
Initiates a WebSocket connection and establishes WebRTC connections as soon as other peers are connected. |
void Disconnect() |
Closes the WebSocket connection and disconnects the WebRTC connections with other peers. |
void SendDataChannelMessage(string message) |
Sends a message via the data channel. |
void SendDataChannelMessageToPeer(string targetPeerId, string message) |
Sends a data channel message to a specific peer. |
void StartVideoTransmission() |
Starts video transmission. |
void StopVideoTransmission() |
Stops video transmission. |
void StartAudioTransmission() |
Starts audio transmission. |
void StopAudioTransmission() |
Stops audio transmission. |
void SetUniquePlayerName(string playerName) |
Sets a unique identifier for the peer. |
Event | Description |
---|---|
WebSocketConnectionChanged |
Triggered when the WebSocket connection state changes. |
WebRTCConnected |
Invoked when a WebRTC connection is successfully established. |
DataChannelConnected |
Raised when the data channel connection is established. |
DataChannelMessageReceived |
Fired when a message is received via the data channel. |
VideoTransmissionReceived |
Triggered when a video stream is received. |
AudioTransmissionReceived |
Triggered when an audio stream is received. |
The WebRTCConnection
component includes several configurable parameters:
[SerializeField] private string WebSocketServerAddress = "wss://unity-webrtc-signaling.glitch.me";
[SerializeField] private string StunServerAddress = "stun:stun.l.google.com:19302";
[SerializeField] private string LocalPeerId = "PeerId"; // must be unique for each peer
[SerializeField] private bool UseHTTPHeader = true; // used for e.g. Glitch.com, because headers are needed
[SerializeField] private bool ShowLogs = true; // mostly for debugging purposes, can be disabled
[SerializeField] private bool ShowDataChannelLogs = true; // mostly for debugging purposes, can be disabled
Modify these values in the Unity Inspector or directly in the script.
Following sample scenes are included in the pacakge:
- WebSocket-TestConnection: For testing the wecksocket connection separately.
- WebRTC-SingleClient-STUNConnection: Testing STUN connection for a single client. Works standalone and can be deployed to clients. Make sure to set the
LocalPeerId
for each client individually. - WebRTC-SingleClient-wLobby-STUNConnection: A simple Lobby example for handling multiple STUN WebRTC clients.
SimpleLobbyManager.cs
shows an example, how to use SimpleWebRTC via C#. - WebRTC-MultipleClients-STUNConnection: Shows how multiple clients can be connected via peer-to-peer connections and share data, video and audio transmissions.
- WebRTC-SingleClient-STUNConnection-PhotonFusion: Testing STUN connection for a single client using Photon Fusion 2 as signaling server. Works standalone and can be deployed to clients. Make sure to set the
LocalPeerId
for each client individually.
WebRTCConnection connection = gameObject.GetComponent<WebRTCConnection>();
connection.Connect(); // Establish WebSocket connection
// after a WebRTC peer-to-peer connection is established
connection.StartVideoTransmission(); // Begin video streaming
connection.SendDataChannelMessage("Hello Peer!"); // Send a message over the data channel
This project is licensed under the MIT License.
Contributions are welcome! Feel free to submit pull requests or report issues.