The Sending.Network iOS SDK provides a messaging framework that enables you to integrate chatroom and notification services within your mobile applications. This quickstart will show you how to set up the SDK and make API calls like user registration, room creation, etc. Let's explore how we would make a simple client that can send and receive messages, create a chat room, get member lists, etc.
You can connect to SDN network by running a local P2P service or connecting to an existing Edge Node.
To build the application that connects to an existing Edge Node, add the following line to the podfile.
pod SendingnetworkSDK/Lite
To build the application that runs a local P2P service, add the following line to the podfile.
pod SendingnetworkSDK
Include the header
importSendingnetworkSDK
Start the local P2P service
If you connect to an existing edge node in the previous step, you can safely skip this step.
To create a local P2P service:
RadixService.shared.start()
Initialize the client
If you choose to connect to an existing Edge Node in the previous step, please contact developer@sending.network for a test node URL and assign it tocredentials.homeserver.
var client:MXRestClient?let credentials =MXCredentials()credentials.node ="http://localhost:65432"self.client =MXRestClient(credentials: credentials, unrecognizedCertificateHandler:nil)
Keeping your access token safe is essential, as it allows complete access to your account.
Log in with your access token
Instantiate a new client object and use the access token to log in:
// Login with the access tokenlet credentials =MXCredentials(homeServer:"http://localhost:65432", userId:"YOUR_USER_ID", accessToken:"YOUR_ACCESS_TOKEN")// Create a clientlet client =MXRestClient(credentials: credentials, unrecognizedCertificateHandler:nil)// Create a session to interact with the server and keep it throughout the app's lifetime// or until the user logs out:let session =MXSession(sendingNetworkRestClient: client)
Sync and listen
Next, we start the client which sets up the connection to the server and performs the first sync, then listen for the response to get the latest state from the server:
// Create a session to interact with the server and keep it throughout the app's lifetime// or until the user logs out:let session =MXSession(sendingnetworkRestClient: client)session.start { response inguard response.isSuccess else {print("sync envnt failed")return } session.listenToEvents(){ event, direction, customObject inswitch direction {case .forwards:// Live/New events come hereif event.eventId !=nil{switch event.wireType{// Got a message from other user.case kMXEventTypeStringRoomMessage:let msg = event.content["body"]as!Stringprint("got message: ", msg, " from: ", event.sender!, "at time: ", event.ageLocalTs)// Send a receipt, then the message won't send again. self.client?.sendReadReceipt(toRoom: event.roomId, forEvent: event.eventId){ response in }break;// Got a room event.case kMXEventTypeStringRoomMember:let ms = event.content["membership"]as!Stringif ms =="invite"{// Invited by a user.print("invited to room: \(event.roomId!) by user: \(event.sender!)") self.join_room(room_id: event.roomId!) }elseif ms =="join"{print("join the room: ", event.roomId) }break;default:print("room event: ", event.roomId, event.eventType, event.type, event.content) } }else{print("room event: ", event) }breakcase .backwards:// Events that occurred in the past will come here when requesting pagination.// roomState contains the state of the room just before this event occurred.break } }}
If you are only interested in some specific type of event, you can set the value of the parameter types:
It's time to invite users to the room you created.
let roomId ="THE_ROOM_ID";let userId ="THER_USER_ID_YOU_WANT_INVITE";client.invite(MXRoomInvitee.userId(userId), toRoom: roomId) { response inswitch response{case .success():print("invite user successfully")returncase .failure(let error):print(error)return }}
Get messages of a room
To get messages, we need to reuse the mxSession instance created before:
// Retrieve the room from its room idlet room = mxSession.room(withRoomId:"YOUR_ROOM_ID")// Add a listener on events related to this room_= room?.liveTimeline.listenToEvents { (event, direction, roomState) inswitch direction {case .forwards:// Live/New events come herebreakcase .backwards:// Events that occurred in the past will come here when requesting pagination.// roomState contains the state of the room just before this event occurred.break }}
Post a message to a room
To post a message, we create a content message and specify a room.
client.sendTextMessage(toRoom:"YOUR_ROOM_ID", text:"Hello World!") { (response) inifcase .success(let eventId)= response {// eventId is for reference// If you have registered events listener like in the previous use case, you will get// notification for this event coming down from the server events stream and// now handled by MXSession. }}