Single blog// see post details
Communicating with REST: The Client/Server Model
- 11219 Views
- 0 Comments
- in Mobile Development
- by admin
The client/server model is a very standard model with which to communicate. When I was first learning about the client/server model, I was using Sockets in C. In general, many programming libraries and tools are just “overlays” on top of more basic tools. PHP was written in (and based on) C, which was written in assembly. This is just how it works with libraries. In iOS, tools are built to interface with very low-level (close to hardware) components. Core Graphics interfaces with the video driver and the video driver interfaces with the screen. When switching to iOS, I assumed I’d be using Sockets, since it was based on C. I didn’t realize that there were industry standards I could use for communication.
Using industry standards is by no means required. However, they’re industry standards for a reason. REST is one of the cleanest, simplest, most efficient ways to open communication between server APIs and iOS apps. Under certain circumstances, REST might not work. For example, if the app needs special treatment that a web server can’t supply, a company might consider having their own communication software designed from the ground up. With that being said, REST relies on software that’s already been written, tested, and secured over the span of many years. If it’s possible to stick to REST then I’d do it.
What is REST? REST stands for Representational State Transfer. In a nutshell, it means that you’re using a web server like Apache or Nginx to communicate with the app. The data transfer works much like a website. To start with, you’ll need a server program that transfers POST or GET data. While writing my own app, I used PHP to send/receive raw post data. Here’s the code I used for it:
It’s a little different than using the $_POST variable, but this is how it should be done. In addition, PHP isn’t the only language that can do this. Java can use Servlets to communicate this way, and C# is another popular language in this particular field. There are a few standard methods of data transfer with REST. My preferred method involves sending JSON data and parsing on each side. However, other options include XML and SOAP. It all depends on your preference. A well-written server might include more methods than just one.
So, we worked out the problem of sending data to the server and getting responses. But how does the server communicate with the app? At first, it doesn’t seem like the server can push data to the app without a request. That’s where Push Notifications come into the picture. The Push notification service is a service provided by Apple. All data passes through Apple servers and is routed onto its respective destination. Android also has a version of Push notifications.
Using a web server to control data flow means that the server doesn’t have to be designed by a large team of experts with experience in handling a large number of connections. A server can reasonably be written by a single person, and can be scaled up very easily. It can also easily be encrypted with SSL, which is a huge benefit in itself. As long as HTTPS is used in the connection URL, this happens automatically without any extra coding. In addition, Push notifications alert users when something is changed on the server side. This is useful for things like messaging, news, updates, and more. However, it means that data caching should be treated differently than if the server had an open pipe to the client. At any rate, REST is the easiest industry-standard method of communication for junior, intermediate and senior developers alike.