1. Introduction
Migrating to Ant Media Server offers several advantages, such as complete control over your infrastructure, robust REST APIs, and support for multiple streaming protocols (WebRTC, RTMP, HLS, SRT, etc.). Whether you want to self-host on-premises or leverage the cloud, Ant Media Server provides the flexibility and customizability that organizations often look for when scaling their real-time streaming solutions.
This guide walks you through a smooth Agora to Ant Media migration using just five essential steps—covering publishing, subscribing, screen sharing, messaging, and multi-user setup.
Whether you’re self-hosting or using the cloud, this migration gives you full ownership of your infrastructure with support for WebRTC, RTMP, HLS, SRT and more.
2. Step-by-Step Migration
This section outlines how to replace Agora’s core services (channels, tokens, front-end publishing/subscribing, screen sharing) with Ant Media Server equivalents.
Agora’s front-end typically looks like:
// Agora
const client = AgoraRTC.createClient({ mode: "rtc", codec: "vp8" });
await client.join(appId, channelName, token, uid);
await client.publish(localTrack);
2.1 Publish Stream
2.1.1 Project Setup
With Ant Media, you’ll replace that logic with WebRTCAdaptor
calls:
- Include AMS’s JavaScript SDK (
webrtc_adaptor.js
) in your project. - Initialize a local or remote dev environment with your AMS WebSocket URL (e.g.,
wss://YOUR_AMS_SERVER:5443/WebRTCAppEE/websocket
). - Token (if used): Make sure your front-end can retrieve the token from your server or
.env
configuration (similar to how you handled Agora’s token logic).
2.1.2 Initializing the WebRTCAdaptor
import { WebRTCAdaptor } from "./webrtc_adaptor.js";
let webRTCAdaptor;
function initWebRTCAdaptor() {
webRTCAdaptor = new WebRTCAdaptor({
websocket_url: "wss://<YOUR_AMS_SERVER>:5443/WebRTCAppEE/websocket",
mediaConstraints: { video: true, audio: true },
localVideoElement: "localVideo", // The HTML <video> ID for local preview
debug: true,
dataChannelEnabled: true, // if you want chat/file transfer, similar to Agora’s RTM
callback: (info, obj) => {
console.log("INFO:", info, obj);
if (info === "initialized") {
// WebRTCAdaptor is ready. You can now publish or play streams.
}
else if (info === "publish_started") {
// Local stream is publishing to AMS
}
else if (info === "screen_share_stopped") {
// If user stops screen sharing, revert to camera, etc.
}
// ...
},
callbackError: (error, message) => {
console.error("WebRTC Error:", error, message);
},
});
}
window.onload = initWebRTCAdaptor;
- Agora:
client.init() / client.join() / client.publish()
- AMS:
new WebRTCAdaptor(...)
, thenpublish(streamId)
2.1.3 Publishing a Stream (Replacing client.join()
/ client.publish()
)
- Assign a
streamId
(returned from the AMS REST call or a user-defined ID). - Optionally pass the token if
Token Control
is enabled.
async function startPublishing() {
const streamId = document.getElementById("streamIdInput").value;
// token is optional, only if your AMS requires it
await webRTCAdaptor.publish(streamId /*, token */);
console.log("Publishing started for", streamId);
}
- This automatically captures your local camera and microphone if
mediaConstraints.video/audio = true
. - The
localVideoElement
specified in the adaptor config receives the local feed preview.
2.1.4 Subscribing to Remote Streams (Replacing client.subscribe()
)
- In Agora, you subscribe to a remote user’s stream, receiving track objects.
- In AMS, call:
async function startPlaying() {
const remoteStreamId = "REMOTE_ID_FROM_SERVER";
await webRTCAdaptor.play(remoteStreamId /*, tokenIfNeeded */);
// The adaptor attaches the remote stream to an HTML <video> with ID=remoteStreamId by default
}
Multi-Participant
- If you used Agora for multi-user calls, each participant in AMS can both publish and play different
streamId
s. - Alternatively, create a single broadcast of type
"conference"
on the AMS side, and each participant joins using that samestreamId
but different user roles.
2.2 Screen Sharing
Agora offers AgoraRTC.createScreenVideoTrack()
to share the screen. In Ant Media, the WebRTCAdaptor
has built-in methods to switch your video source mid-publish:
function startScreenShare() {
// Switch from camera to screen share
webRTCAdaptor.switchDesktopCapture(streamId);
}
function stopScreenShare() {
// Revert to camera capture
webRTCAdaptor.switchVideoCameraCapture(streamId, selectedCameraId);
}
Key Points:
- If you want a picture-in-picture (PiP) style (screen + webcam overlay), call
switchDesktopCaptureWithCamera(streamId)
. - Browsers typically prompt the user to choose a screen or window.
- Once screen sharing is stopped, the
callback
eventscreen_share_stopped
fires, letting you revert your UI if needed.
2.3 Data Channels (Replacing Agora’s RTM or Channel Messaging)
If you used Agora for in-channel text chat or data messages:
- In AMS, enable
dataChannelEnabled: true
in your adaptor config. - Send data: jsCopyEdit
function sendData() { const myMessage = document.getElementById("chatBox").value; webRTCAdaptor.sendData(streamId, myMessage); }
- Receive data in
callback
: jsCopyEditcallback: (info, obj) => { if (info === "data_received") { console.log("Data received: ", obj.data); // Show in chat UI } }
You can even send binary data (images, files), similarly to how you might with Agora’s more advanced RTM features.
2.4 Multi-Participant Conferencing
For 1-on-1 or small group calls, each user can:
- Publish their own
streamId
. - Play others’
streamId
s.
Alternatively, if you want a single “room” approach:
- Create a broadcast with
"type":"conference"
via the AMS REST API. - Each participant calls
webRTCAdaptor.joinRoom(streamId, token)
or publishes/plays accordingly. - AMS automatically handles the SFU (Selective Forwarding Unit) approach, so participants receive each other’s streams in real-time.
2.5 Recording & Playback
- Agora has a separate Cloud Recording or On-Premise Recording service.
- AMS:
- Enable recording in the AMS dashboard or via REST.
- View your
.mp4
or.m3u8
(HLS) files in AMS’sstreams/
directory. - Play recorded streams with any standard player that supports MP4/HLS.
3. Summary
With these extended steps, you can replicate Agora features (channel creation, front-end publish/subscribe, screen share, chat) using Ant Media Server. The key transformations include:
- Backend: Replace “channel creation” with AMS’s
POST /broadcasts/create
. Use optional tokens for security instead of Agora’s token builder. - Front-End: Switch from
AgoraRTC.createClient()
toWebRTCAdaptor
methods:publish(streamId)
,play(streamId)
,switchDesktopCapture()
, etc. - Advanced:
- Screen Sharing is done via
switchDesktopCapture(streamId)
or PiP withswitchDesktopCaptureWithCamera(streamId)
. - Data Channels replicate chat/file transfer.
- Conference Mode in AMS can replicate Agora’s group calls.
- Screen Sharing is done via
By following these steps and leveraging Ant Media Server’s robust features—like multi-protocol broadcasting, token-based security, and server-side plug-ins—you can seamlessly migrate your entire Agora-based workflow to a self-hosted, highly customizable real-time streaming infrastructure.
If you have questions or need any support for your migration, contact us via a form, or schedule a meeting to have a coffee and chat, or write directly to contact@antmedia.io