Skip to content

Repository files navigation

StompNet

StompNet is an asynchronous STOMP 1.2 client library for .NET 4.5 and .NET Standard 1.5.

Features

  • High level interfaces to ease the flow of the application.
  • Built using the observer pattern. It can be used for reactive application approaches. This is a great library to be used together with Reactive Extensions library.
  • Thread-safe.
  • Library user would rarely have to interact with STOMP frames directly, unless it is desired.
  • Message receipt confirmations and sequence number generation for frames are automatically handled by the library.

Installation

On your Package Manager Console: Install-Package StompNet

Usage

The following code is an example to show the basic usage of StompNet.

// Example: Send three messages before receiving them back.staticasyncTaskReadmeExample(){// Establish a TCP connection with the STOMP service.using(TcpClienttcpClient=newTcpClient()){awaittcpClient.ConnectAsync("localhost",61613);//Create a connector.using(IStompConnectorstompConnector=newStomp12Connector(tcpClient.GetStream(),"localhost",// Virtual host name."admin","password")){// Create a connection.IStompConnectionconnection=awaitstompConnector.ConnectAsync();// Send a message.awaitconnection.SendAsync("/queue/example","Anybody there!?");// Send two messages using a transaction.IStompTransactiontransaction=awaitconnection.BeginTransactionAsync();awaittransaction.SendAsync("/queue/example","Hi!");awaittransaction.SendAsync("/queue/example","My name is StompNet");awaittransaction.CommitAsync();// Receive messages back.// Message handling is made by the ConsoleWriterObserver instance.awaittransaction.SubscribeAsync(newConsoleWriterObserver(),"/queue/example");// Wait for messages to be received.awaitTask.Delay(250);// Disconnect.awaitconnection.DisconnectAsync();}}}

The observer ConsoleWriterObserver class for handling the incoming messages is:

classConsoleWriterObserver:IObserver<IStompMessage>{// Incoming messages are processed here.publicvoidOnNext(IStompMessagemessage){Console.WriteLine("MESSAGE: "+message.GetContentAsString());if(message.IsAcknowledgeable)message.Acknowledge();}// Any ERROR frame or stream exception comes through here.publicvoidOnError(Exceptionerror){Console.WriteLine("ERROR: "+error.Message);}// OnCompleted is invoked when unsubscribing.publicvoidOnCompleted(){Console.WriteLine("UNSUBSCRIBED!");}}

For more examples, take a look at the examples project where you can see how to:

  • Use receipt confirmations automatically.
  • Send byte array messages and set its content type.
  • Set acknowledgement mode for subscriptions.
  • Use low level APIs to directly read/write frames into the stream.

Comments about the usage of the library.

  • IO stream connection management is not handled by the library. If the TCP connection fails, a new IStompConnector must be created.
  • Exceptions will be thrown by currently awaited tasks and they will also be notified to observers through the OnError method.
  • One and only one of the methods OnError or OnCompleted will be invoked. Both methods indicate the end of the incoming messages stream.
  • SubscribeAsync returns a Task that can be used to unsubscribe at any time by calling DisposeAsync. If an unsubscription is not done explicitly, unsubscription is done when the connector is disposed or an exception occurs in the stream.
  • All methods may receive a CancellationToken. Anyway, cancelling before a method is finished may leave the IO stream in an invalid state.

License

Apache License 2.0

ToDo

  • Heartbeat support
  • STOMP 1.1 support

About

Asynchronous STOMP 1.2 client library for .NET 4.5 and .NET Standard 1.5.

Resources

Stars

20 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages