How to Send & Receive Data

For the remaining integration steps, it is recommended that you have already developed a simple game template that requires a small amount of data to be exchanged. Otherwise, follow along by exchanging some small sample data. Here’s how you can send data using the sendData function:

Step 1: sendData

Here’s how you can send data using the sendData function.

Pass in your data to this function as an NSData object to be sent to our Skillz servers which will be forwarded to the opponent’s device.

Make sure to pass in all the data that’s needed to ensure that the game will stay in sync on both devices. The size limit is 2 KB. 

Note: The bytes passed to sendData above will be sent to the Sync game server and forwarded to your opponent’s onDidReceiveData implementation:

Recommended: For the data you send, have an object type for each type of message you send so that you can easily handle the different types of data when it reaches the other device. JSON serialization will work, but is likely not the most performant way to send data.

Recommended: Handle finishing states for the match in onMatchCompleted and onDidReceiveData.

Note: Reliability is guaranteed through disconnects, but the system maintains a cap on data stored in this state. Reliability is only guaranteed up to a certain point when your opponent is disconnected. As soon as you receive notification that a player’s  opponent has been disconnected, you should stop attempting to send messages in order to ensure no data is lost. Also, if you find you are sending more than 30 messages a second, you may find that some messages could be lost on disconnect/reconnect and game state may need to be reconciled on reconnection.


- (void)sendBoardToOpponent
    NSUInteger index = self.pendingMoveSelection.tag;
    NSString *newBoard = [self formatNewBoard:index];
    self.board = newBoard;
    [self sendDataToServer];

- (void)sendDataToServer
    [[Skillz skillzInstance] sendData:[self.board    dataUsingEncoding:NSUTF8StringEncoding]];

Step 2: onDidReceiveData

Here is how you can receive data using the onDidReceiveData function. This function needs to accept an NSData object and is called whenever it receives data.


- (void)onDidReceiveData:(NSData *)data
    NSString *boardState = [[NSString alloc] initWithData:data

    if (!self.isGameHost && [self isLessCompleteBoard:boardState]) {
       [self sendDataToServer];

    self.board = boardState;

    dispatch_sync(dispatch_get_main_queue(), ^{
        [self updateBoardDisplay];
        [self.turnLabel setText:[self isMyTurn] ? @"Your Turn" : [NSString stringWithFormat:@"%@'s Turn", self.opponent.displayName]];

        if ([self checkWinner:opponentSymbol] || [self checkWinner:playerSymbol] || ![self.board containsString:@"-"]) {

            [self endGamePlay];

Next Step: Handle Game States