Skip to content

robin-io/robin.io-go

Repository files navigation

Robin.io-go

Table of contents

Table of Contents
  1. Introduction
  2. Prerequisites
  3. Getting Started
  4. Sending your first message
  5. License

Introduction

Robin.io-go is The official Go SDK built to communicate with the Robinapp API. Now you can integrate Robin.io with minimal effort and quickly setup a real-time messaging platform in your Web application.

Prerequisites

The following packages are required to use the sdk:

  • go 1.16
  • Robin api_key

Getting started

Step 1: Create a Robinapp account

A Robinapp account comprises everything required in a chat service including users, message, and api-keys. To create an application:

  1. Go to the Robinapp Dashboard and enter your email and password, and create a new account.
  2. Navigate to Api Config and copy your API key

Note: All the data is limited to the scope of a single user account, thus the users in different Robinapp accounts are unable to chat with each other.

Step 2: Install the SDK

  go get robin.io-go

Sending your first message

Follow the step-by-step instructions below to authenticate and send your first message.

Authentication

To use the features of the Chat SDK in your client app, a robin instance must be initiated in each client app before user authentication with Robin server. These instances communicate and interact with the server based on an authenticated user account, allowing for the client app to use the Chat SDK features.

Step 1: Initialize the Chat SDK

To initialize a Robin instance, pass the API key as the first argument to in the Robin() method, You can find your API key in the API Configuration tab in your Robin Account.

Then true or false for as the second parameter as it tells the sdk whether to load with ssl or not.

robin := Robin{
		Secret: "NT-qBsdC.....kOBVYRr",
		Tls:    true,
	}

Step 2: Connect to Robin server

You'll need a USER_TOKEN to connect to the Robin server.

A. Create User Token

Create user token

...
token, err := robin.CreateUserToken(UserToken{MetaData: map[string]interface{}{
		"name": "michael",
	}})

if err != nil {
    println(err)
}

Connect to the Robin server. To connect to the robin server you need to generate a session token.

sessionToken, err := robin.getSession("<USER_TOKEN>")
// the sessionToken returned gets stored in the robin instance and is accessed by the methods that require it
// call back for a successful connection
func connected(socket gowebsocket.Socket) {}
// call back for an unsuccessful connection
func disconnected(err error, socket gowebsocket.Socket) {}
// call back for when a message is recieved via the connection
func text_recieved(msg string, socket gowebsocket.Socket) {}

user_token := "<USER_TOKEN>"

conn, err := robin.Connect(user_token, connected, nil, disconnected, text_recieved, nil, nil, nil)

if err != nil {
    println(err)
}

Step 3: Channels

All messages sent via Robin are sent through channels, you can consider channels as tunnels that relay messages to all connected clients.

Step 4: Create a conversation

Before we can send a message to a channel we first need to create a converstion.

robin := Robin{
    Secret: "NT-QuNtKo......JwGrymaVxQX",
    Tls:    true,
}

// func (*Robin).CreateConversation(senderName string, senderToken string, receiverToken string, receiverName string) (ConversationResponseData, error)

conv, err := robin.CreateConversation("YFXOK....BaqKgDWOhE", "YFXOKV...gDWOhE", "jesse")

if err != nil {
    fmt.Println(err)
}

// create group conversation

notify := Robin{
    Secret: "NT-QuNtKolp.....cJwGrymaVxQX",
    Tls:    true,
}


// func (*Robin).CreateGroupConversation(name string, moderator UserToken, participants []UserToken) (ConversationResponseData, error)


conv, err := notify.CreateGroupConversation("The council",
    UserToken{UserToken: "YFXOKVyKBGv...KgDWOhE"},
    []UserToken{{UserToken: "TKSSAK...gDWOhE"},
    })

if err != nil {
    t.Error(err)
}

Step 5: Subscribe to a channel

To send and recieve messages in Robin, we utilize channels, your connection has to be subscribed to a channel to send a recieve messages through and from it.

err := robin.Subscribe("<channel_name>")
if err != nil {
    fmt.Println(err)
}

Step 6: Send a message

Finally, send a message to a conversation

err := robin.SendMessageToConversation("<channel_name>", map[string]interface{}{
    "msg":"Hello from Robin",
}, "TKSHSqww....aA", "Elvis")

Options

The following are general attributes used in Robin:

Attribute Type Default Description
socket gowebsocket.Socket nil Websocket object returned after calling robin.connect()
sender_name String '' Name of the person sending the message
sender_token String '' USER_TOKEN of the person sending the message
receiver_name String '' Name of the person receiving the message
receiver_token String '' USER_TOKEN of the person receiving the message
msg map[string]interface{} {} Json serializable object containing the message

If you have any comments or questions regarding bugs and feature requests, visit Robinapp community.

View Documentation Here.

License

Distributed under the MIT License. See LICENSE for more information.