README.md
author Mahlon E. Smith <mahlon@martini.nu>
Wed, 27 Oct 2021 14:36:25 -0700
changeset 10 d63cce6d1a09
parent 3 d0bc42746346
permissions -rw-r--r--
Updates for more modern (v1.6.0) nim.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
3c678f7bf1a0 README.md edited online with Bitbucket
Mahlon Smith <mahlon@martini.nu>
parents:
diff changeset
     1
# README #
3c678f7bf1a0 README.md edited online with Bitbucket
Mahlon Smith <mahlon@martini.nu>
parents:
diff changeset
     2
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     3
## Overview ##
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     4
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     5
This is a pure-nim client library for interacting with Stomp
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     6
compliant messaging brokers.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     7
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     8
https://stomp.github.io/
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
     9
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    10
Stomp is a simple protocol for message passing between clients, using a central
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    11
broker.  It is a subset of other more elaborate protocols (like AMQP), supporting
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    12
only the most used features of common brokers.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    13
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    14
Because this library is pure-nim, there are no external dependencies.  If you
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    15
can compile a nim binary, you can participate in advanced messaging between processes.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    16
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    17
A list of broker support for Stomp can be found here:
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    18
https://stomp.github.io/implementations.html.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    19
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    20
This library has been tested with recent versions of RabbitMQ.  If it
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    21
works for you with another broker, please let the author know.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    22
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    23
### Installation ###
1
3c678f7bf1a0 README.md edited online with Bitbucket
Mahlon Smith <mahlon@martini.nu>
parents:
diff changeset
    24
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    25
The easiest way to install this module is via the nimble package manager, 
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    26
by simply running 'nimble install stomp'.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    27
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    28
Alternatively, you can fetch the 'stomp.nim' file yourself, and put it
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    29
in a place of your choosing.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    30
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    31
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    32
### Protocol support ###
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    33
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    34
This library supports (almost) the entirety of the Stomp 1.2 spec,
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    35
with the exception of client to server heartbeat.  Server to client
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    36
heartbeat is fully supported, which should normally be sufficient to
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    37
keep firewall state tables open and sockets alive.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    38
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    39
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    40
### Callbacks ###
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    41
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    42
Because a client can receive frames at any time, most of the behavior
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    43
of this module is implemented via callback procs.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    44
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    45
By default, most every event is a no-op.  You can override various
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    46
behaviors with the following callbacks:
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    47
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    48
* **connected_callback**:  Called when the Stomp library makes a successful
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    49
 connection to the broker.
1
3c678f7bf1a0 README.md edited online with Bitbucket
Mahlon Smith <mahlon@martini.nu>
parents:
diff changeset
    50
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    51
* **error_callback**: Called if there was a **ERROR** frame in the stream. By default,
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    52
 this raises a **StompError** exception with the error message.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    53
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    54
* **heartbeat_callback**: Called when a server heartbeat is received.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    55
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    56
* **message_callback**: Called when a **MESSAGE** frame is received.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    57
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    58
* **missed_heartbeat_callback**: Called when the Stomp socket is idle longer than
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    59
 the specified heartbeat time -- usually an indication of a problem.  The default behavior
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    60
 raises a **StompError** exception.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    61
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    62
* **receipt_callback**: Called when a **RECEIPT** frame is received.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    63
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    64
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    65
### Custom headers ###
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    66
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    67
Depending on the broker, you may be able to add addtional features to outgoing messages
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    68
by adding specific headers.  You can also add "x-headers" that are carried between messages.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    69
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    70
Another use is to issue "receipts" on sends or subscriptions, to ensure the broker has
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    71
processed your request.  Here's an example of how to perform receipt processing:
1
3c678f7bf1a0 README.md edited online with Bitbucket
Mahlon Smith <mahlon@martini.nu>
parents:
diff changeset
    72
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    73
```
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    74
#!nimrod
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    75
proc accept_receipt( c: StompClient, r: StompResponse ) =
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    76
   var receipt = r[ "receipt-id" ]
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    77
   # ... match this receipt up to the request that generated it
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    78
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    79
var client = newStompClient( socket, "..." )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    80
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    81
client.receipt_callback = accept_receipt
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    82
client.connect
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    83
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    84
var headers = seq[ tuple[name:string, value:string] ]
3
d0bc42746346 Documentation pass.
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    85
headers.add( ("x-breakfast", "tequila") )
d0bc42746346 Documentation pass.
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
    86
headers.add( ("receipt", "special-identifier") )
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    87
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    88
client.send( "/destination", "message!", "text/plain", headers )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    89
```
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    90
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    91
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    92
### Transactions ###
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    93
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    94
This library has full support for transactions.  Once entering a
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    95
transaction, any messages or acknowledgments attached to it must be
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    96
committed before the broker will release them.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    97
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    98
With one open transaction, messages are automatically attached to it.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
    99
If you have multiple open transactions, you'll need to add which one
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   100
you want a message to be part of via the custom headers.
1
3c678f7bf1a0 README.md edited online with Bitbucket
Mahlon Smith <mahlon@martini.nu>
parents:
diff changeset
   101
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   102
```
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   103
#!nimrod
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   104
# Single transaction
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   105
#
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   106
client.begin( "trans-1" )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   107
client.send( "/destination", "hi" ) # Part of "trans-1"
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   108
client.send( "/destination", "yo" ) # Part of "trans-1"
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   109
client.send( "/destination", "whaddup" ) # Part of "trans-1"
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   110
client.commit # or client.abort
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   111
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   112
# Multiple simultaneous transactions
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   113
#
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   114
client.begin( "trans-1" )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   115
client.begin( "trans-2" )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   116
client.begin( "trans-3" )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   117
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   118
var headers = seq[ tuple[name:string, value:string] ]
3
d0bc42746346 Documentation pass.
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   119
headers.add( ("transaction", "trans-1") )
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   120
client.send( "/destination", "hi", nil, headers ) # Part of "trans-1"
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   121
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   122
headers = @[]
3
d0bc42746346 Documentation pass.
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   123
headers.add( ("transaction", "trans-2") )
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   124
client.send( "/destination", "hi", nil, headers ) # Part of "trans-2"
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   125
client.ack( "some-ack-id", "trans-2" ) # Part of "trans-2"
1
3c678f7bf1a0 README.md edited online with Bitbucket
Mahlon Smith <mahlon@martini.nu>
parents:
diff changeset
   126
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   127
headers = @[]
3
d0bc42746346 Documentation pass.
Mahlon E. Smith <mahlon@martini.nu>
parents: 2
diff changeset
   128
headers.add( ("transaction", "trans-3") )
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   129
client.send( "/destination", "hi", nil, headers ) # Part of "trans-3"
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   130
client.ack( "some-ack-id", "trans-3" ) # Part of "trans-3"
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   131
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   132
client.abort( "trans-1" )  # anything "trans-1" never happened
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   133
client.commit( "trans-2" ) # "trans-2" messages and acks are released
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   134
client.abort( "trans-3" )  # anything "trans-3" never happened
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   135
```
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   136
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   137
### Example ###
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   138
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   139
This is a complete client that does the following:
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   140
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   141
* Connect to an AMQP server at **mq.example.com** via SSL as the **test** user,
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   142
  in the **/example** vhost.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   143
* Request heartbeats every **5** seconds.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   144
* Subscribe to a topic exchange **events** with the key of **user.create**, requiring message
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   145
  acknowledgement.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   146
* Accept incoming messages, parsing the JSON payloads.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   147
* If parsing was successful, ACK the message and emit a new message to the exchange
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   148
  with JSON results to the **user.created** key -- presumably to be picked up by another
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   149
  process elsewhere.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   150
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   151
```
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   152
#!nimrod
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   153
# (This should be compiled with -d:ssl)
1
3c678f7bf1a0 README.md edited online with Bitbucket
Mahlon Smith <mahlon@martini.nu>
parents:
diff changeset
   154
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   155
import
10
d63cce6d1a09 Updates for more modern (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
   156
   std/[net,json],
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   157
   stomp
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   158
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   159
let
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   160
   socket = newSocket()
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   161
   sslContext = newContext( verifyMode = CVerifyNone )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   162
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   163
sslContext.wrapSocket( socket )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   164
var client = newStompClient( socket, "stomp+ssl://test:test@mq.example.com/%2Fexample?heartbeat=5" )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   165
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   166
# Announce when we're connected.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   167
proc connected( c: StompClient, r: StompResponse ) =
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   168
   echo "Connected to a ", c["server"], " server."
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   169
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   170
# Echo to screen when we see a heartbeat.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   171
proc heartbeat( c: StompClient, r: StompResponse ) =
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   172
   echo "Heartbeat at: ",  c.last_msgtime
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   173
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   174
# Parse JSON, perform work, send success message.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   175
proc message( c: StompClient, r: StompResponse ) =
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   176
   let id = r[ "ack" ]
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   177
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   178
   if r[ "content-type" ] != "application/json":
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   179
	   echo "I expect JSON payloads."
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   180
	   c.nack( id )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   181
	   return
1
3c678f7bf1a0 README.md edited online with Bitbucket
Mahlon Smith <mahlon@martini.nu>
parents:
diff changeset
   182
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   183
   try:
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   184
	   var json = r.payload.parse_json
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   185
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   186
	   # ... do the heavy lifting with the parsed data.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   187
	   # ... and assuming is was successful, ack and emit!
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   188
	   
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   189
	   c.ack( id )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   190
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   191
	   var message = %*{ "user": json["user"].getStr, "otherstuff": true }
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   192
	   c.send( "/exchange/events/user.created", $message, "application/json" )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   193
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   194
   except JsonParsingError:
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   195
	   echo "Couldn't parse JSON! ", r.payload
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   196
	   c.nack( id )
1
3c678f7bf1a0 README.md edited online with Bitbucket
Mahlon Smith <mahlon@martini.nu>
parents:
diff changeset
   197
2
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   198
# Attach callbacks
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   199
client.connected_callback = connected
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   200
client.message_callback = message
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   201
client.heartbeat_callback = heartbeat
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   202
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   203
# Open a session with the broker
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   204
client.connect
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   205
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   206
# Subscribe to a topic key, requiring acknowledgements.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   207
client.subscribe( "/exchange/events/user.create", "client-individual" )
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   208
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   209
# Enter message loop.
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   210
client.wait_for_messages
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   211
```
48806dfcbba5 Update README.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   212