📅  最后修改于: 2023-12-03 14:40:13.658000             🧑  作者: Mango
If you're building a WebRTC application, you'll need to use a TURN server to relay media between peers when they cannot establish a direct connection. Coturn is an open-source TURN server that can be run in a Docker container, making deployment and scaling easier.
To install Coturn Docker, run the following command:
docker pull coturn/coturn
This will pull the latest Coturn Docker image from Docker Hub.
To run Coturn Docker, you can use the following command:
docker run -d --network="host" coturn/coturn
This will start Coturn Docker in daemon mode and use the host network to communicate with the host machine. You can customize your Coturn Docker configuration by specifying environment variables with the -e
flag. For example:
docker run -d --network="host" -e TURN_USERNAME=myuser -e TURN_PASSWORD=mypassword coturn/coturn
This command sets the TURN server's username and password to myuser
and mypassword
, respectively.
Once you have Coturn Docker up and running, you can use it in your WebRTC application. You'll need to configure your WebRTC library or framework to use your Coturn server's IP address and ports for TURN server communication.
Here is an example configuration for the popular WebRTC library, simple-peer:
const peer = new SimplePeer({
initiator: true,
trickle: false,
config: {
iceServers: [
{ urls: "turn:<your-coturn-ip-address>:<your-coturn-port>", username: "myuser", credential: "mypassword" },
{ urls: "stun:stun.l.google.com:19302" }
]
}
});
peer.on("signal", (data) => {
console.log("Signal data:", data);
});
peer.on("connect", () => {
console.log("Connected!");
});
peer.on("data", (data) => {
console.log("Received data:", data.toString());
});
This example uses myuser
and mypassword
as the TURN server credentials and <your-coturn-ip-address>
and <your-coturn-port>
as the Coturn Docker container's IP address and port, respectively.
Coturn Docker simplifies TURN server deployment and scaling for WebRTC applications. By using Coturn Docker, you can easily spin up a TURN server in a container and integrate it into your application. Plus, since Coturn is open source, you can contribute to the project and improve the TURN server for everyone.