run into some issues that are not covered on this post. More specifically, if you're running anything that looks older than those numbers, consider upgrading the packages.
First, you'll need to setup react-native-webrtc. There's a well documented guide on the
to get it working. For Android, it's
so you don't run into any issues. However these are the errors I experienced which are NOT documented on the guide:
Second, install sip.js. As documented
, you'll have to import 'registerGlobals()' then call this function before attmepting to create a UserAgent. I prefer to do this in the index.js file, before registering the app entry point.
That's all you need to know about setting it up. From here, getting a phonecall working is specific to what kind of server you're running. In my experience, it worked with an Asterisk server.
Here is a working example of a context which tracks the state of the UserAgent the stream:
import React, {useState, useEffect} from 'react';
import {UserAgent, Registerer, SessionState, Invitation} from 'sip.js';
const SipClientContext = React.createContext();
const SipClientProvider = ({children}) => {
const [userAgent, setUserAgent] = useState(null);
const [registered, setRegistered] = useState(false);
const [session, setSession] = useState(null);
/**
* Invitation is a Session
* @param {Invitation} invitation
*/
function onInvite(invitation) {
// An Invitation is a Session
const incomingSession = invitation;
incomingSession.stateChange.addListener(newState => {
switch (newState) {
case SessionState.Establishing:
// Implement a loader here
break;
case SessionState.Established:
setSession(incomingSession);
/**
* incomingSession is the stream. So you can set this as a state and pass the stream to the RTCView to get video and audio.
*
* Example:
* ```
if (session !== null && !session.sessionDescriptionHandler)
{
setStream(session.sessionDescriptionHandler.remoteMediaStream);
}
```
then in the component:
```
<RTCView
streamURL={stream.toURL()}
style={styles.stream} />
```
*/
break;
case SessionState.Terminated:
setSession(null);
break;
default:
break;
}
});
let constrainsDefault = {
audio: true,
video: true,
};
const options = {
sessionDescriptionHandlerOptions: {
constraints: constrainsDefault,
},
};
incomingSession.accept(options);
}
useEffect(() => {
const userAgentOptions = {
/**
* credentials go here
*/
delegate: {
onInvite: onInvite,
},
transportOptions,
uri,
};
const ua = new UserAgent(userAgentOptions);
const registerer = new Registerer(ua);
ua.start()
.then(() => {
registerer.register();
setUserAgent(ua);
setRegistered(true);
})
.catch(error => {
console.error('Error registering with Asterisk server:', error);
setRegistered(false);
});
return () => {
if (userAgent) {
userAgent.stop();
}
if (session) {
session.bye();
}
};
}, []);
return (
<SipClientContext.Provider value={{userAgent, registered, session}}>
{children}
</SipClientContext.Provider>
);
};
export {SipClientContext, SipClientProvider};
tl;dr, read the documentation. If it doesn't work, upgrade your packages.