SENDBIRD API INTEGRATION WITH LARAVEL
In this article I will be integrating the SendBird API into a Laravel based website. This will require some novice knowledge of Laravel such as working with the file system, views and some other basic Laravel functionality. When using this API you will also need to understand the basics of using a REST API.
GETTING STARTED
The first thing you need to do is wait for the API key to become available after creating your new app.
I would like to let you know that a message comes up and you will wait for it to go away before you can start making calls to the API. What they don’t tell you is that it’s not a dynamic call, so you will need to refresh the page or you’ll be waiting all day for it to go away.
SendBird API calls are all REST calls, like almost every other API in the world right now, so it should be pretty easy to integrate this into our Laravel site. The SendBird API requires two things in the headers when you call an API:
- Content-Type
- API-Token
The Content-Type is application/json, charset=utf8 and the API-Token will be the token given to you after you have created your application, which you can find on the main page of the SendBird dashboard of your application.
OUR APPLICATION
The application we will be building is a chat room that the website users can socialize. At this point we should be looking into the SendBird API and finding the API we will need. Proper planning is what makes great software, finding what calls we need now will help us later when we start building our codebase. Below are some of the API I have found that will be useful in our application:
- Creating users
- Get a list of current users
- Let the user update their information
- View a user’s information
- Sending messages to an open channel
While these calls will be what we want, there are plenty of other API we can use and integrate later on. We may even overlook some of the things we need at this point but that’s fine, let’s just call it the agile approach.
Normally what I do is write out my classes in their own directory with a test file that will call all the API before I integrate it into my existing system. We aren’t going to do that here, here we will write these classes directly into the Laravel directory structure so we won’t have to worry about the integration later. Laravel does a good job in autoloading out classes for us if they are in the correct place.
We are going to add a directory to the app directory in the Laravel directory. Our folder will be named SendBird. The file we are going to create will also be named the same as our directory, this will let Laravel autoload the class for us when we use it. Our file is going to be named SendBird.php. The class will be named Chat and the namespace will be named SendBird.
We can instantiate the class in our view:
EXTERNAL API REQUESTS WITH LARAVEL
When requesting an external API (like we are going to do here) you can use just about anything you want. I am not a Laravel expert, maybe they have something that will help you do this but many people use Guzzle (Guzzle Github; Guzzle Docs) or simply make CURL requests. Some SendBird calls are POST and some are GET, you should always check the documentation to make sure you are using the write method when calling an API.
To install Guzzle with composer execute the following command in the base directory of your application (the base Laravel directory):
composer require guzzlehttp/guzzle
Once Guzzle is installed we can simply supply the namespaces in our class to use it for the REST API calls to SendBird. When you initialize Guzzle you will want to supply a base URI to SendBird. The base URI is: https://{region_identifier}.sendbird.com/v3. Where “region identifier” is, will be found on your SendBird dashboard overview page. In my case it simply says, api, so my base URI will be https://api.sendbird.com/. It would also be wise to have our Content-Type and API Token in our class so we can reference them easier since every request needs them.
Our class constructor will instantiate the Guzzle client with our base URI. Once the base URI is set this way it will be easier for us to make REST calls to SendBird. We already looked at some of the API we will need earlier, now we need to add the calls into our class.
CREATE THE CHANNEL
SendBird has an API for an Open Channel. This is the public chat our users send messages into when they submit their message. In the dashboard you can create a channel by clicking on open channels and then clicking the plus sign to add a new channel. I will create a channel named pubwith the channel url named public_channel.
SENDING MESSAGES TO OPEN CHANNELS
I wish I could say that I could finish this post but apparently this is a premium feature of the SendBird API, so I am sorry to say even as a developer I don’t have access to these features even though I have 1000 requests left. Sad but true, money hungry API developers win again. So I will leave you with my code so far, I wish I would have known these features weren’t in the free package I signed up with and I refuse to pay to explore API.
Source:https://slashersoftware.wordpress.com/2018/02/18/sendbird-api-integration-with-laravel/
The post Sending messages using SENDBIRD API in Laravel appeared first on Laravel Interview Questions.
Source: laravelinterviewquestions.com