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.
In this chapter, we will learn more about the structure and features of the AMPS JavaScript client and build our first JavaScript program using AMPS.
The AMPS client library is packaged as a single file, amps.js
in development environments, or amps.min.js
for production. Every JavaScript application you build will need to reference this file, and the file must be deployed along with your application in order for your application to function properly. Alternatively, for applications that are distributed through NPM, the client library can be listed as a dependency, and thus installed automatically.
There are several ways of including the JavaScript client in order to use it. Depending on your project's structure and environment, you can include the client in one of the following ways.
In case of TypeScript and manual installation, you need to reference the typing file (provided within the client package) at the top of the file:
The AMPS JavaScript NPM package already includes typings, so that the library can be seamlessly used in native TypeScript projects.
Once the library in included in the HTML file, it becomes available as a global object, either as amps
or window.amps
.
Let's begin by writing a simple program that connects to an AMPS server and sends a single message to a topic. This code will be covered in detail just following the example.
In the example above, we show the entire program; but future examples will isolate one or more specific portions of the code.
Let us now revisit the code we listed above:
In the above example and throughout this guide we use the modern JavaScript syntax which is widely supported and typically is used in projects with a build/deployment system, such as webpack
, angular-cli
, and create-react-app
. However, the JavaScript client library fully supports the obsolete syntax used in older browsers and projects without a build system as well. Below is the same program, written in the classic ES5 syntax.
When using the default authenticator, the AMPS clients support the standard format for including a username and password in a URI, as shown below:
When provided in this form, the default authenticator provides the username and password specified in the URI. If you have implemented another authenticator, that authenticator controls how passwords are provided to the AMPS server.
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 wss
.
ws
(WebSocket) and wss
(WebSocket Secure) are the only supported transports for the JavaScript client.
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 ws
and wss
, the address consists of a host name and port number. In this case, the host address is localhost:9007
.
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.
Message Type - 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.
The JavaScript client does not support legacy protocols, the amps
protocol is required.
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 and protocols.