|
| 1 | +# Why PubSub |
| 2 | + |
| 3 | +Abstraction of various (cloud) pub/sub systems. |
| 4 | + |
| 5 | +## Interface |
| 6 | + |
| 7 | +A quick glance: |
| 8 | + |
| 9 | +```haxe |
| 10 | +interface Publisher<Message> { |
| 11 | + function publish(message:Message):Promise<Noise>; |
| 12 | +} |
| 13 | +
|
| 14 | +interface Subscriber<Message> { |
| 15 | + function subscribe(handler:Envelope<T>->Void):Subscription; |
| 16 | +} |
| 17 | +
|
| 18 | +interface Envelope<Message> { |
| 19 | + final content:Outcome<Message, Error>; |
| 20 | + function ack():Void; |
| 21 | + function nack():Void; |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +Define two interfaces containing all the publishers and subscribers needed. |
| 28 | +Then, pick an implementation class and use the interfaces as type parameters, wrapped by `why.PubSub` |
| 29 | + |
| 30 | +```haxe |
| 31 | +var amqp = new why.pubsub.amqp.Amqp<MyPubSub>(...); |
| 32 | +
|
| 33 | +typedef MyPubSub = why.PubSub<Publishers, Subscribers>; |
| 34 | +
|
| 35 | +interface Publishers { |
| 36 | + @:pubsub({serialize: v -> haxe.Serializer.run(v)}) |
| 37 | + @:pubsub.amqp({exchange: 'foo', routingKey: ''}) |
| 38 | + var foo(get, never):Publisher<{foo:Int, bar:String}>; |
| 39 | + |
| 40 | + |
| 41 | + @:pubsub({serialize: v -> haxe.Serializer.run(v)}) |
| 42 | + @:pubsub.amqp({exchange: 'cache', routingKey: 'cache.$id'}) |
| 43 | + @:pubsub.cache({key: id + foo + bar}) |
| 44 | + function cache(id:String, foo:Int, bar:Bool):Publisher<{foo:Int, bar:String}>; |
| 45 | +} |
| 46 | +
|
| 47 | +interface Subscribers { |
| 48 | + @:pubsub({unserialize: v -> tink.core.Error.catchExceptions(haxe.Unserializer.run.bind(v))}) |
| 49 | + @:pubsub.amqp({queue: 'bar', prefetch: 2}) |
| 50 | + var bar(get, never):Subscriber<{foo:Int, bar:String}>; |
| 51 | + |
| 52 | + |
| 53 | + @:pubsub({unserialize: v -> tink.core.Error.catchExceptions(haxe.Unserializer.run.bind(v))}) |
| 54 | + @:pubsub.amqp({queue: 'cache_$id', prefetch: 2}) |
| 55 | + @:pubsub.cache({key: id + foo + bar}) |
| 56 | + function cache(id:String, foo:Int, bar:Bool):Subscriber<{foo:Int, bar:String}>; |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Implementations |
| 61 | + |
| 62 | +#### `why.amqp.Amqp` |
| 63 | + |
| 64 | +Node.js only. Based on the npm package `amqplib` for the AMQP 0-9-1 protocol. Compatible to RabbitMQ. |
| 65 | + |
| 66 | +#### `why.local.Local` |
| 67 | + |
| 68 | +A simple in-memory message queue. Mainly for local testing. |
| 69 | + |
| 70 | +## Metadata |
| 71 | + |
| 72 | +**`@:pubsub` on Publisher: (required for all implementations)** |
| 73 | + |
| 74 | +```haxe |
| 75 | +{ |
| 76 | + // serialize message into binary |
| 77 | + final serialize:T->Chunk; |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +**`@:pubsub` on Subscriber: (required for all implementations)** |
| 82 | + |
| 83 | +```haxe |
| 84 | +{ |
| 85 | + // unserialize binary into message |
| 86 | + final unserialize:Chunk->Outcome<T, Error>; |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +**`@:pubsub.cache` on Publisher & Subscriber: (optional for all implementations)** |
| 91 | + |
| 92 | +Only applicable to functions. If not specified, every time the function gets called, a new instance will be returned. Otherwise, the returned value will be cached by the key specified. |
| 93 | + |
| 94 | +```haxe |
| 95 | +{ |
| 96 | + // cache key. function arguments are accessible here |
| 97 | + final key:String; |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +**`@:pubsub.local` on Publisher: (required for `Local`)** |
| 102 | + |
| 103 | +```haxe |
| 104 | +{ |
| 105 | + // queue name |
| 106 | + final name:String; |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +**`@:pubsub.local` on Subscriber: (required for `Local`)** |
| 111 | + |
| 112 | +```haxe |
| 113 | +{ |
| 114 | + // queue name |
| 115 | + final name:String; |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +**`@:pubsub.amqp` on Publisher: (required for `Amqp`)** |
| 120 | + |
| 121 | +```haxe |
| 122 | +{ |
| 123 | + // name of the exchange to publish a message |
| 124 | + final exchange:String; |
| 125 | + |
| 126 | + // routing key to publish a message |
| 127 | + final routingKey:String; |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +**`@:pubsub.amqp` on Subscriber: (required for `Amqp`)** |
| 132 | + |
| 133 | +```haxe |
| 134 | +{ |
| 135 | + // name of the queue to publish a message |
| 136 | + final queue:String; |
| 137 | + |
| 138 | + // max "in-flight" messages for this subscription (see amqp doc for more info) |
| 139 | + final prefetch:Int; |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +## TODO |
| 144 | + |
| 145 | +- [] GCP PubSub |
| 146 | +- [] AWS SQS |
0 commit comments