kraftfahrstrasse - TypeScript WAMP client library for node/browsers
![Join the chat at https://gitter.im/Verkehrsministerium/kraftfahrstrasse](https://badges.gitter.im/verkehrsministerium/kraftfahrstrasse.svg)
Introduction
kraftfahrstrasse
is another WAMP implementation, which is fully driven by Typescript.
This introduces type safety to the WAMP universe for the first time. We are very proud, we did this.
We also used the newest language features, which mades the code more readable and better to maintain.
So, keep in mind, that shipping to ES5 is not intended to work.
We never tested that and we don't want to.
Our applications, which depend on kraftfahrstrasse
ship ES6 and it works fine.
We also want to keep the bundle size as low as possible, which is made possible by shipping ES6 modules and
rely on ES6 tree shaking.
We have dependencies like msgpack5
and ws
which are installed but don't used until you import the modules.
This results in a much smaller bundle size. We aim for a bundle size of 50kB
, which is 5x smaller than autobahn-js
.
This is the reason why our configuration seems a bit messy. You need to import the transport, serializer and
authentication provider into your main file before you can use it. If you import them, Webpack 4 will include them into
your bundle, if not, it wont. This is a huge advantage for your bundle size.
Design goals
- Fast
- Feature complete
- Clean and concise usage (i.e. no polymorphic callbacks)
- Simple configuration
- Full static type safety
- Best interopability with
autobahnkreuz
and the service
libraries, to provide a seamless WAMP experience
Usage Example
The example below is intended to be run in node, if you'd like to run it in the browser change NodeWebSocketTransport
to BrowserWebSocketTransport
.
import {
AnonymousAuthProvider,
Connection,
JSONSerializer,
NodeWebSocketTransport,
} from '@verkehrsministerium/kraftfahrstrasse';
const connection = new Connection({
endpoint: "ws://localhost:4000",
realm: "realm01",
serializer: new JSONSerializer(),
transport: NodeWebSocketTransport,
transportOptions: {
},
authProvider: new AnonymousAuthProvider(),
logFunction: console.log as any,
});
const main = async () => {
await connection.Open();
const sub = await connection.Subscribe(
"com.example.topic",
(args, kwargs, details) => {
console.log("Subscription:", args, kwargs, details);
},
{}
);
await sub.Unsubscribe();
const pub = await connection.Publish(
"com.example.topic",
["Hello World"],
{},
{
"acknowledge": true,
}
);
const id = await pub.OnPublished();
console.log('Published event as', id);
const reg = await connection.Register(
"com.example.rpc",
async (args, kwargs, details) => {
console.log('Got invoked:', args, kwargs, details);
return {
args,
kwargs,
};
},
{}
);
await reg.Unregister();
const call = connection.Call(
"com.example.rpc",
["Hello, World"],
{},
{},
);
const result = await call[0];
console.log('Got call result:', result);
await connection.CancelCall(result[1], 'kill');
}
await main();
Configuration
Parameter | Type | Description |
---|
endpoint | string | endpoint describes the URL of your router. |
realm | string | realm describes the realm, which should the client use. |
transport | ITransport | transport describes a class, which is used to transport the data. WAMP supports multiple ways to connect to the router. Common transports are BrowserWebSocketTransport or NodeWebSocketTransport . Make sure, you import them correctly. |
serializer | ISerializer | serializer describes an instance of ISerializer, which is used to serialize the protocol data for transport. Common serializers are JSON and MsgPack. Make sure, you import them correctly. |
authProvider | IAuthProvider | authProvider describes an instance of IAuthProvider, which is supposed to handle the authentication to the router. Common authProvider are Anonymous and Ticket or resume token. |
logFunction | logFunction? | Pass a custom function to include the output of kraftfahrstrasse into your own log management or log style. |
Transports
At the moment, we only offer WebSocket based transports. We have one transport for nodejs (using the ws library)
and another transport for browsers, using the native WebSocket object.
HTTP LongPoll and rawsocket transports may be implemented against this interface, however we feel like most people focus
on WebSocket transports.
In the sections of the transports, the possible options are described.
BrowserWebSocketTransport
No further configuration is supported in a browser context.
NodeWebSocketTransport
The NodeWebSocketTransport uses the ws library under the hood, so head over to their documentation for an up-to-date list of configuration options.
The transportOptions
dict will be passed directly as options
argument into the ws
constructor.
Typically, you would like to set a custom CA, client certificates for TLS Authentication or connect timeout here.
Serializers
At the moment, we offer a JSONSerializer which is portable across node and the browser, as well as a portable MSGPack
serializer, which relies on msgpack5.
Both serializers confirm to the WAMP spec, however we provide the possibility to extend or write a custom serializer.
JSONSerializer
This serializer uses the JSON
object present in nodeJS and modern browsers to encode and decode json. It follows the WAMP specification for binary message handling.
The subprotocol for the json serializer is: wamp.2.json
.
BrowserMSGPackSerialzer/NodeMSGPackSerializer
This serializer uses msgpack5 to provide a more space-efficient serialization. It uses the wamp.2.msgpack
subprotocol.
Attention: This serializer handles binary data as an extension to allow de-/reencoding of binary data at the server side to JSON.
It is therefore NOT compatible with the crossbar.io router.
If you absolutely need binary data, you can write your own msgpack serializer which is compatible with crossbar.io.
Authentication
WAMP offers different authentication methods, we honor this fact by providing a simple interface which allows us to extend
and customize the authentication process.
WAMP distinguishes between transport level and protocol level authentication, with transport level being TLS Client Authentication,
Cookie Authentication or Anonymous. Transport level authentications don't involve the calculation or presence of a shared
secret or challenge/response at protocol level but instead rely on the transport below the protocol to handle this.
AnonymousAuthProvider
AnonymousAuthProvider represents the anonymous
authentication method, it allows the user to specify a username
(with a fallback on anonymous
if none is provided).
TicketAuthProvider
TicketAuthProvider represents the ticket
authentication method, using a shared secret to authenticate.
TicketAuthProviders are used when using kraftfahrstrasse
along with autobahnkreuz to authenticate
users.
When using the TicketAuthProvider, the password will not be stored within the AuthProvider, but instead the AuthProvider
calls a user defined function to calculate the ticket based on the server issued details object.
CookieAuthProvider
The CookieAuthProvider is a transport-level authentication provider, which means that this connection is identified by the client who presented cookies at the connection establishment procedure.
No further configuration is required.
TLSAuthProvider
The TLSAuthProvider is a transport-level authentication provider, which means that this connection is identified by the client who presented a TLS client certificate at the connection establishment procedure.
No further configuration is required.
We recommend using the TLSAuthProvider for backend components.