• Stars
    star
    158
  • Rank 237,131 (Top 5 %)
  • Language
    TypeScript
  • Created over 3 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A typesafe framework to destroy client-server barriers.

RocketRPC πŸš€ - A typesafe framework to destroy the client-server wall.

Frame 8175 (1)

RocketRPC is typesafe RPC library which gets out of your way. Define methods in your server, which you can access instantly in your client - complete with auto-completions and type-checking.

Usage

RocketRPC.Demo.mp4

Talk

Click on the image below to watch @akash-joshi talk about RocketRPC at Svelte London:

Talk

Installation

Install the package

npm i rocketrpc

Client

Note: On the client side, all functions return a Promise with the result by default, because of the asynchronous nature of sockets. So, all passed functions are also modified to return a Promise.

import { Client } from "rocketrpc";
import { API } from "../server";

const client = Client<API>("http://localhost:8080");

const { listFiles, prisma } = client;

const main = async () => {
  // use prisma on the client
  console.log(await prisma.user.findMany());

  // passing multiple parameters to the function
  console.log(await client.sum(12, 20));

  // get server details
  console.log(await listFiles());
};

main();

Server

import { Server } from "rocketrpc";
import { PrismaClient } from "@prisma/client";

import listFiles from "./apis/listFiles";

const api = {
  // initialize Prisma once
  prisma: new PrismaClient();

  sum: (x: number, y: number) => x + y,

  // Fetch all files on server
  listFiles,
};

export type API = typeof api;

Server(8080, api);

Error Handling

At the moment, any error on the server-side is sent to std:error and thrown on the client side.

Try running the examples locally!

Metadata Context

Socket Client

The socket.io client being used by rocketRPC is accessible via the _rocketRpcContext key.

How does it work internally?

In short, the library depends on Websockets, Object Proxies, and Typescript generics to work. In detail:

1. Websockets

We use socket.io for fast and reliable socket connections. Websockets can be lighter than HTTP requests when a large number of connections are needed. Also, they have a smaller code footprint than HTTP requests. Their usage is anyways abstracted away in the codebase, and they can be replaced with any other technology if needed.

2. Object Proxies

The framework utilizes Object Proxies get control over the client object. Any function call made on a property of the client object (or on a deconstructed property), like

client.functionOne();

// or

const { functionOne } = client;
functionOne();

is handled by a get property which has been set on the Object Proxy here.

You can go through the code to see how it uses the property name and parameters to make a socket call to the server.

3. Typescript Generics

All of the auto-complete goodness that the framework provides throughout the app depends on Typescript generics. On the server side, the type is directly applied on the API object,

const api: API = { ...yourApi };

while on the client side it's passed to the Client initializer.

const client = Client<API>(endpoint);

The client function is actually a generic, which accepts the type provided by the user and applies Promise to the return type of each of them. It's a very Typescript-specific piece of code but you can read it here.

Sponsors

If you find RocketRPC enjoyable to work with and wish to show your support for the project, you can express your gratitude by sponsoring it through GitHub Sponsors!

Furthermore, if your company is currently utilizing RocketRPC and would like to support its long-term maintenance, please refer to the sponsorship tiers.

RocketConnect
RocketConnect

Contributing

Pull requests are welcome. You'll probably find lots of improvements to be made.

Open issues for feedback, requesting features, reporting bugs or discussing ideas.

More Repositories

1

local-storage-hook

React Hook for Local Storage
JavaScript
28
star
2

fcc-socket-client

JavaScript
25
star
3

howitzer

Generate, explain and execute commands in the CLI
JavaScript
13
star
4

gatsby-query-params

Get query params of the current URL in Gatsby
JavaScript
10
star
5

clean-hunt

The Power of ProductHunt search without a Sea of Dead Links.
JavaScript
9
star
6

madulus

A telegram bot to automate parts of your life.
TypeScript
9
star
7

json-transformer

Transform & Edit JSON live in your browser
JavaScript
6
star
8

socket-server

Server repo for the app built in the blog
JavaScript
6
star
9

respond

A frontend framework to use useState and useEffect with template strings.
TypeScript
6
star
10

neural-color-picker

JavaScript library to help you pick colors by using a Recurrent Neural Network
JavaScript
6
star
11

avstream.js

JavaScript library leveraging the MediaRecorder API to allow streaming of audio or video files to server.
HTML
5
star
12

socket-blog-client

The client application built in the blog
JavaScript
5
star
13

deno-url-shortener

A URL shortener allowing expiry at a date, using Deno.Land
TypeScript
5
star
14

scratchnotes

πŸ“Take notes in style using React
CSS
5
star
15

Open-Chat

An open-source chatting app built on socket.io and node
JavaScript
4
star
16

nextjs-config

πŸ”₯ My nextjs config with eslint and styled-components
CSS
4
star
17

Felix-Apology-Services

A website for Pewdiepie's Apology Service
HTML
2
star
18

notes-app-for-blog

CSS
2
star
19

scraper-pro-max

Scrape a list of web-pages into a single HTML output.
JavaScript
2
star
20

auth-wiki-public

This repository was made to demonstrate functionality of Mongo and Auth0 with React using NextJS.
JavaScript
2
star
21

react-dogs-app

JavaScript
2
star
22

superhero

πŸš€ A SuperHero search tool built in React
JavaScript
2
star
23

stylish

πŸ’…πŸŒ  An app to build beautiful React styled components visually
CSS
1
star
24

HCAHPS-Hospital-Rating

Calculate rating of Hospitals using node.js
JavaScript
1
star
25

blogproject

Project to create and view blogs using node and mySQL
JavaScript
1
star
26

checkout-reviews

TypeScript
1
star
27

akash

πŸ€ my npm namecard
JavaScript
1
star
28

lets-code

Jupyter Notebook
1
star
29

sentiment-bot

Python
1
star
30

pwa-repo

JavaScript
1
star
31

bluejack

A blackjack implementation in React and Typescript
TypeScript
1
star
32

conway-game-of-life

Conway's game of life implemented in functional JS + React.
JavaScript
1
star
33

myblog

This is the HTML for my blog
JavaScript
1
star
34

adversarial-comparer

Jupyter Notebook
1
star
35

TE-Assignments

An unorganized dump of assignments done by me in my 6th semester of Engineering
C
1
star