GithubHelp home page GithubHelp logo

thruway.js's Introduction

Thruway.js

This project is a WAMP v2 client written in TypeScript that uses RxJS v5 Observables instead of promises and event emitters. It's designed to work with modern frontend frameworks like Angular v2/4 as well as node.js.

If you don't know what WAMP is, you should read up on it.

If you don't know what RxJS or ReactiveExtensions is, you're missing out...

note: This library is stable, but may not have all of the WAMP features implemented. Since this project originated as an internal project for Voryx, the features are limited to only the ones that were needed.

Installation

npm install thruway.js
npm install rxjs
npm install ws // only when using with Node

Usage

import {Client} from "thruway.js";

const wamp = new Client('ws://localhost:9090', 'realm1');

Call

wamp.call('add.rpc', [1, 2])
    .map((r: ResultMessage) => r.args[0])
    .subscribe(r => console.log(r));

Register

wamp.register('add.rpc', (a, b) => a + b).subscribe();

Publish to topic

wamp.publish('example.topic', 'some value');
wamp.publish('example.topic', Observable.interval(1000)); // you can also publish an observable

Subscribe to topic

wamp.topic('example.topic').subscribe((v)=>console.log(v));

Angular Example

Create a wamp service

import {Injectable} from '@angular/core';
import {Client} from 'thruway.js';

@Injectable()
export class WampService extends Client {
    constructor() {
        super('wss://demo.crossbar.io/ws', 'realm1');
    }
}

Inject and use the service in your component

import {Component} from '@angular/core';
import {WampService} from '../wamp.service';
import {Observable} from 'rxjs/Observable';
import {EventMessage} from 'thruway.js/src/Messages/EventMessage';

@Component({
    selector: 'app-counter',
    template: '<span>{{counter | async}}</span>'
})
export class CounterComponent {
    counter: Observable<number> = this.wamp
        .topic('com.myapp.counter')
        .map((r: EventMessage) => r.args[0]);

    constructor(private wamp: WampService) {}
}

Node Example

const Thruway = require("thruway.js");
const Rx = require("rxjs");

const wamp = new Thruway.Client('wss://demo.crossbar.io/ws', 'realm1');

wamp.publish('com.myapp.counter', Rx.Observable.interval(1000));

PHP Example

Install the RxThruway Client

composer require rx/thruway-client
<?php

use Rx\Observable;
use Rx\Thruway\Client;

require __DIR__ . './vendor/autoload.php';

$wamp = new Client('wss://demo.crossbar.io/ws', 'realm1');

$wamp->publish('com.myapp.counter', Observable::interval(1000));

thruway.js's People

Contributors

davidwdan avatar mbonneau avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.