AMPS uses the name of the client as a session identifier and as part of the identifier for messages originating from that client.
For this reason, when a transaction log is enabled in the AMPS instance (that is, when the instance is recording a sequence of publishes and attempting to eliminate duplicate publishes), an AMPS instance will only allow one application with a given client name to connect to the instance.
When a transaction log is present, AMPS requires the client name for a publisher to be:
Unique within a set of replicated AMPS instances
Consistent from invocation to invocation if the publisher will be publishing the same logical stream of messages
If publishers do not meet this contract (for example, if the publisher changes its name and publishes the same messages, or if a different publisher uses the same session name), message loss or duplication can happen.
60East recommends always using consistent, unique client names. For example, the client name could be formed by combining the application name, an identifier for the host system, and the ID of the user running the application. A strategy like this provides a name that will be different for different users or on different systems, but consistent for instances of the application that should be treated as equivalent to the AMPS system.
Likewise, if a publisher is sending a completely independent stream of messages (for example, a microservice that sends a different, unrelated sequence of messages each time it connects to AMPS), there is no need for a publisher to retain the same name each time it starts. However, if a publisher is resuming a stream of messages (as in the case when using a file-backed publish store), that publisher must use the same client name, since the publisher is resuming the session.
When a client logs on to AMPS, the client sends AMPS a username and password. The username is derived from the URI, using the standard syntax for providing a username in a URI. For example, tcp://JohnDoe:@server:port/amps/messagetype
to include the username JohnDoe
in the request.
For a given username, the password is provided by an Authenticator
. The AMPS client distribution includes a DefaultAuthenticator
that simply returns the password, if any, provided in the URI. A logon()
command that does not specify an Authenticator
will use an instance of DefaultAuthenticator
.
If your authentication system requires a different authentication token, you can implement an Authenticator
that provides the appropriate token.
When specifying a URI for connection to an AMPS server, you may specify a number of transport-specific options in the parameters section of the URI connection parameters. Here is an example:
In this example, we have specified the AMPS instance on localhost
, port 9007
, connecting to a transport that uses the amps
protocol and sending JSON messages. We have also set two parameters: tcp_nodelay
, a Boolean (true/false) parameter, and tcp_sndbuf
, an integer parameter. Multiple parameters may be combined to finely tune settings available on the transport. Normally, you'll want to stick with the defaults on your platform, but there may be some cases where experimentation and fine-tuning will yield higher or more efficient performance.
The AMPS client supports the value of tcp
in the scheme component connection string for TCP/IP connections, and the value of tcps
as the scheme for SSL encrypted connections.
Starting with version 5.3.3.0, the AMPS client supports creating connections over both IPv4 and IPv6 protocols if supported by the underlying Operating System.
By default, the AMPS client will prefer to resolve host names to IPv4 addresses, but this behavior can be adjusted by supplying the ip_protocol_prefer
transport option, described in the table below.
The following transport options are available for TCP connections:
Option
Description
bind
(IP address) Sets the interface to bind the outgoing socket to.
Starting with version 5.3.3.0, both IPv4 and IPv6 addresses are fully supported for use with this parameter.
tcp_rcvbuf
(integer) Sets the socket receive buffer size. This defaults to the system default size. (On Linux, you can find the system default size in /proc/sys/net/core/rmem_default
.)
tcp_sndbuf
(integer) Sets the socket send buffer size. This defaults to the system default size. (On Linux, you can find the system default size in /proc/sys/net/core/wmem_default
.)
tcp_nodelay
(boolean) Enables or disables the TCP_NODELAY
setting on the socket. By default TCP_NODELAY
is disabled.
tcp_linger
(integer) Enables and sets the SO_LINGER
value for the socket By default, SO_LINGER
is enabled with a value of 10
, which specifies that the socket will linger for 10 seconds.
tcp_keepalive
(boolean) Enables or disables the SO_KEEPALIVE
value for the socket. The default value for this option is true.
ip_protocol_prefer
(string) Influence the IP protocol to prefer during DNS resolution of the host. If a DNS entry of the preferred protocol can not be found, the other non-preferred protocol will then be tried.
If this parameter is not set, the default will be to prefer IPv4.
If an explicit IPv4 address or IPv6 IP address is provided as the host, the format of the IP address is used to determine the IP protocol used and this setting has no effect.
Supported Values:
ipv4
: Prefer an IPv4 address when resolving the host
ipv6
: Prefer an IPv6 address when resolving the host
This parameter is available starting with version 5.3.3.0.
The connection string can also be used to pass logon parameters to AMPS. AMPS supports the following additional logon option:
Option
Description
pretty
Provide formatted representations of binary messages rather than the original message contents.
Once your application is built, you will need to think about how to deploy it to additional computers. With your application’s dependency on AMPS.Client.dll
, you need to include AMPS.Client.dll
along with your application. The most straightforward way to accomplish this is to install AMPS.Client.dll
into the same folder as your .exe
file. For example, if you distribute your executable in a zip file that users are expected to unpack, simply include AMPS.Client.dll
assemblies into that zip file. When your executable runs, Windows will attempt to load AMPS.Client.dll
from the same directory as your executable, and if it is not found, your executable will fail to run.
If your organization develops and deploys many AMPS applications and would like more centralized control over the maintenance of these AMPS client deployments, consider installing AMPS.Client.dll
into the Global Assembly Cache ("GAC"). The GAC allows you to share one copy of an assembly — like the AMPS client — across many applications on a computer. This technique requires that the assembly have a strong name, and that you use an installer that places AMPS.Client.dll
into the GAC. Installing the assembly in the GAC is not recommended unless many applications will share an AMPS client. For more information on the GAC, follow the link http://msdn.microsoft.com/en-us/library/yf1d93sz.aspx to the Microsoft Developer Network documentation on the GAC.
You are now able to develop and deploy an application in C# that publishes messages to AMPS. In the following chapters, you will learn how to subscribe to messages, use content filters, work with SOW caches and fine-tune messages that you send.
In this chapter, we will learn more about the structure and features of the AMPS C# library, and build our first C# program using AMPS.
The AMPS client is packaged as a single managed assembly, AMPS.Client.dll
You can find AMPS.Client.dll
in the AMPS/bin
directory of your AMPS C# client. Every .NET application you build will need to reference this assembly file, and the assembly must be deployed along with your application in order for your application to function properly.
Let's begin by writing a simple program that connects to an AMPS server and publishes a single message to a topic:
In the example above, we show the entire program; but future examples will isolate one or more specific portions of the code. The next section describes how to build and run the application and explains the code in further detail.
To build this program, create a new C# command-line project in Visual Studio and add a reference to AMPS.Client.dll
using the "Add Reference..." option in Visual Studio. Replace the code in Program.cs
with the code in the example above, then modify the client.connect()
on line 14 with the address and port of your AMPS server. Now you should be able to compile and execute the code, and if the AMPS server is running, the message Hello world
is published to the messages topic. If an error occurs, an exception will be written to the console.
If the message is published successfully, there is no output to the console. We will demonstrate how to create a subscriber to receive messages in the next chapter.
Let us now revisit the code we listed earlier.
The AMPS clients use connection strings to determine the server, port, transport, and protocol to use to connect to AMPS. When the connection point in AMPS accepts multiple message types, the connection string also specifies the precise message type to use for this connection.
Connection strings have a number of elements:
As shown in the figure above, connection strings have the following elements:
Transport - Defines the network used to send and receive messages from AMPS. In this case, the transport is tcp
. For connections to transports that use the Secure Sockets Layer (SSL), use tcps
. For connections to AMPS over a Unix domain socket, use unix
.
Host Address - Defines the destination on the network where the AMPS instance receives messages. The format of the address is dependent on the transport. For tcp
and tcps
, the address consists of a host name and port number. In this case, the host address is localhost:9007
. For unix
domain sockets, a value for hostname and port must be provided to form a valid URI, but the content of the hostname and port are ignored, and the file name provided in the path parameter is used instead (by convention, many connection strings use localhost:0
to indicate that this is a local connection that does not use TCP/IP).
Protocol - Sets the format in which AMPS receives commands from the client. Most code uses the default amps
protocol, which sends header information in JSON format. AMPS supports the ability to develop custom protocols as extension modules, and AMPS also supports legacy protocols for backward compatibility.
MessageType - Specifies the message type that this connection uses. This component of the connection string is required if the protocol accepts multiple message types and the transport is configured to accept multiple message types. If the protocol does not accept multiple message types, this component of the connection string is optional, and defaults to the message type specified in the transport.
Legacy protocols such as fix
, nvfix
and xml
only accept a single message type, and therefore do not require or accept a message type in the connection string.
As an example, a connection string such as:
would work for programs connecting from the local host to a Transport
configured as follows:
See the AMPS Configuration Guide for more information on configuring transports.