So what is an API? - High Level Summary
- Basically an API is a process that allows two systems (client ↔ server, server ↔ server, or server ↔ databases or even DB ↔ DB) to connect
- It is a set of rules and protocols for building and interacting with software applications. APIs allow different software systems to communicate with each other, enabling them to share data and functionalities.
- Think of it like a waiter in a restaurant that connects the Customers to Food (from the Kitchen)
There are multiple application level protocols such as HTTP, WebSocket, FTP (File Transfer Protocol ), SMTP ( Secure Mail Transfer Protocol ), SSH (Secure Shell) → Connect to a different machine and run the CLI. Think of it as Humans having multiple languages (each one has its own rules: grammer, syntax, vocab, pronunciation, etc)
- Each server’s port runs on some default ports, and awaits incoming requests and deals w/ them as they come. Here are the default ports for examples below :
- HTTP → 80, WebSocket → , FTP → 21, SMTP → 25 , SSH → 22
- This lesson we’ll be focusing on HTTP and WebSocket
Client-Server Model
The client-server model, as we've discussed so far, often presents the client as a user utilizing a web browser to issue a request to a server. The server, typically another computer, responds to these requests. However, it's crucial to understand that the client in a client-server model doesn't necessarily have to be a web browser
- Client: The client is an application or system that accesses a service made available by a server. The client can be a web browser, an email software, an app on your phone, or any other software that needs to access some service. The term "client" can also refer to the device running this application. For example, your computer or smartphone can be a client when you're browsing the internet. The client typically initiates communication, sending a request to the server for specific information or services.
- Server: The server is a computer, device, or software that provides resources, data, services, or functionality to the client or other servers on a network. Servers wait for incoming requests from clients and respond by fulfilling those requests. For instance, a web server delivers web pages to clients, an email server handles sending and receiving emails, and a database server provides clients with access to database services.
Based on context, the roles of client and server can interchange. For instance, a single machine can function both as a client, requesting resources, and a server, providing resources. This is observable when you enter a search query into Google, where google.com may request data from a third party, thereby assuming the role of a client. Peer-to-peer (P2P) networks offer another example, where a machine can simultaneously act as both a client and a server.
REMOTE PROCEDURE CALL (RPC)
- Essentially we want to excecute code that is not on a single local machine… So how do we do this?
- using an RPC, allows us the ability for a program to perform functions on a separate machine using a connection via a network
- Using the RPC, it may look like the netowrk is doing local work (i.e. put google.com/neetcode/listvideos in your search bar), but actually most of that code is written on the other machine ! —> We’re just making call to YouTube's servers in the background to retrieve the relevant information.
HTTP
- Hypertext Transfer Protocol (HTTP) - The protocol of the Internet
- HTTP is built on top of IP and TCP, its the one devs have control over
- Recall, IP sends packets of data from the source to the destination, but it doesn't worry about the order in which packets arrive. TCP, which is built on top of IP, ensures that the packets are delivered in the right order.
- HTTP sits on top of TCP. At its core, it is a request/response protocol. It is a set of rules for how data should be formatted and transmitted over the web, along with how the servers and browsers should respond to different commands.