|
| 1 | +# @superbalist/js-pubsub-http |
| 2 | + |
| 3 | +An HTTP adapter for the [js-pubsub](https://github.com/Superbalist/js-pubsub) package. |
| 4 | + |
| 5 | +[](https://twitter.com/superbalist) |
| 6 | +[](https://travis-ci.org/Superbalist/js-pubsub-http) |
| 7 | +[](LICENSE) |
| 8 | +[](https://www.npmjs.com/package/@superbalist/js-pubsub-http) |
| 9 | +[](https://www.npmjs.com/package/@superbalist/js-pubsub-http) |
| 10 | + |
| 11 | +This adapter assumes that you have a HTTP service which accepts an array of messages POSTed to a |
| 12 | +/messages/(channel) end-point. |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install @superbalist/js-pubsub-http |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | +```node |
| 22 | +'use strict'; |
| 23 | + |
| 24 | +let pubsub = require('@google-cloud/pubsub'); |
| 25 | +let GoogleCloudPubSubAdapter = require('@superbalist/js-pubsub-google-cloud'); |
| 26 | +let HTTPPubSubAdapter = require('@superbalist/js-pubsub-http'); |
| 27 | + |
| 28 | +process.env.GOOGLE_APPLICATION_CREDENTIALS = '/path/to/your-gcloud-key.json'; |
| 29 | + |
| 30 | +// create the underlying adapter which is going to be decorated |
| 31 | +let client = pubsub({ |
| 32 | + projectId: 'your-project-id-here', |
| 33 | +}); |
| 34 | + |
| 35 | +let googleCloudAdapter = new GoogleCloudPubSubAdapter(client); |
| 36 | + |
| 37 | +// now create our decorator |
| 38 | +// the decorator will proxy subscribe calls straight to the googleCloudAdapter |
| 39 | +// publish calls will be POSTed to the service uri |
| 40 | +let adapter = new HTTPPubSubAdapter('http://127.0.0.1', googleCloudAdapter); |
| 41 | + |
| 42 | +adapter.subscribe('my_channel', (message) => { |
| 43 | + console.log(message); |
| 44 | + console.log(typeof message); |
| 45 | +}); |
| 46 | + |
| 47 | +// consume messages |
| 48 | +// note: this is a blocking call |
| 49 | +adapter.subscribe('my_channel', (message) => { |
| 50 | + console.log(message); |
| 51 | + console.log(typeof message); |
| 52 | +}); |
| 53 | + |
| 54 | +// publish messages |
| 55 | +adapter.publish('my_channel', {first_name: 'Matthew'}); |
| 56 | +adapter.publish('my_channel', 'Hello World'); |
| 57 | + |
| 58 | +// publish multiple messages |
| 59 | +let messages = [ |
| 60 | + 'message 1', |
| 61 | + 'message 2', |
| 62 | +]; |
| 63 | +adapter.publishBatch('my_channel', messages); |
| 64 | +``` |
| 65 | + |
| 66 | +## TODO |
| 67 | + |
| 68 | +* Add unit tests |
| 69 | + |
| 70 | +## Examples |
| 71 | + |
| 72 | +The library comes with [examples](examples) for the adapter and a [Dockerfile](Dockerfile) for |
| 73 | +running the example scripts. |
| 74 | + |
| 75 | +Run `make up`. |
| 76 | + |
| 77 | +You will start at a `bash` prompt in the `/usr/src/app` directory. |
| 78 | + |
| 79 | +If you need another shell to publish a message to a blocking consumer, you can run `docker-compose run js-pubsub-http /bin/bash` |
| 80 | + |
| 81 | +To run the examples: |
| 82 | +```bash |
| 83 | +$ ./run_consumer.sh |
| 84 | +$ ./run_publisher.sh (in a separate shell) |
| 85 | +``` |
0 commit comments