Quick Start
note
This document assumes installation is complete, and you're
using the async/await API with a modern TS/JS compiler.
Connecting to Maglev Infrastructure#
Step one is to create a Maglev instance. In this case we'll use the demo
Relay. You'll want to deploy your own relay if you need an SLA. Keep in mind
that Maglev is still in Alpha though.
- TypeScript
- JavaScript
Connections are established lazily, so this doesn't actually do anything on it's
own. If you need to wait for the Maglev object to connection to the relay, you
can await maglev.reachable.whenEqual(true).
Talking to Another Node#
Talking to another node is very easy. All you need is that node's unique ID (or a matching Node Tag result), and Maglev handles all the messy relaying and direct-connection negotiation.
- TypeScript
- JavaScript
Connections#
Connections are managed by the Maglev client itself. When you get a reference to another node, the Maglev client will immediately begin connection negotiation and authentication with that node. Traffic can be sent over the first connection established (which is almost always a relayed connection through a Maglev relay). In the background the client will attempt to 'upgrade' the connection to a direct-connect. The switch over is transparent, apart from the likely large reduction in latency. This works even in browsers.
Defining an RPC#
Maglev RPCs are defined in proto3 format. Take a moment to familiarize yourself with it's syntax if it's new to you.
Confused about what an RPC is, what this syntax means, and why it's useful? We have an entire page on that here. Think of them like strongly typed REST endpoint definitions. Except better in every possible way.
Create a greeter.proto file somewhere. We recommend keeping all .proto files
in a directory called protos at the root of your project.
Compiling Protobuf Files#
Proto syntax isn't understood by a JavaScript interpreter, so a compiler is used
to translate the proto definition into something that the Maglev core library
can use at runtime. It also produces TypeScript type definitions for strong
end-to-end typing. This is a good command to add to your package.json build
scripts.
- TypeScript
- JavaScript
caution
Maglev is built from the ground up to support strong typing from end to end and is itself written in TypeScript. Pure JavaScript support is provided by running the generated TS code through the TypeScript compiler, which results in rather unreadable code. We strongly encourage you to use TypeScript if you have the option.
Hosting an RPC Endpoint#
Maglev does not disambiguate "server" and "client" instances. Any node on the Maglev network can both serve and/or call RPC endpoints. This is made possible by not having to expose 'ports' like you would on a traditional TCP server. Lets setup a basic RPC handler for our "Greeter Service" defined above.
- TypeScript
- JavaScript
caution
This demo uses ES module syntax, a relatively modern version is being used for
source code. While other module types are supported (see maglev gen js --help)
their use is currently undocumented.
Calling an RPC Endpoint#
The other side of calling a Maglev RPC endpoint is even easier, thanks to the generated code the Maglev CLI produces.
- TypeScript
- JavaScript