Interface MessageService


public interface MessageService
Service for asynchronous communication between clients. All messages are sent over "channels" identified by a unique string ID, so that clients can send and retrieve only the messages they are interested in. Within each channel it is also possible to make sure a message only reaches a specific user. The service also keeps track of the users currently logged in to the server where the service is running.
  • Method Details

    • isOpen

      boolean isOpen()
      Returns true if the service is open and able to send and receive messages.
      Returns:
      true if the service is open and able to send and receive messages.
    • subscribe

      void subscribe(String channelID, MessageListener l)
      Subscribes to messages sent on a channel.
      Parameters:
      channelID - The ID of the channel.
      l - The listener to receive new messages.
    • unsubscribe

      void unsubscribe(String channelID, MessageListener l)
      Unsubscribes a listener from messages on a channel.
      Parameters:
      channelID - The ID of the channel.
      l - The listener to remove.
    • subscribeToServerCalls

      void subscribeToServerCalls(Consumer<String> l)
      Subscribes to the server callback channel. This channel is only used to let the server call clients to inform, warn or report an error.
      Parameters:
      l - Consumer of the server message. The message is json and always contains
      • a string property "category" with the valid values "info", "warn" and "error"
      • a string property "domain" denoting the type, or "domain" of the warning/error/info
      • a property "message" which can be any json-object
      E.g. a message string may look like
       {
         "domain" : "server.load",
         "category" : "info",
         "message": "server.load.ok"
       } or 
       {
         "domain" : "mail.notification",
         "category" : "warn",
         "message": {
            "code" : "mail.notification.error",
            "unsent" : ["bill@domain.com", "alice@florb.com"]
         }
       }
    • unsubscribeFromServerCalls

      void unsubscribeFromServerCalls(Consumer<String> l)
    • publish

      void publish(String channelID, Map<String,String> message)
      Publishes a message on a channel.
      Parameters:
      channelID - The ID of the channel.
      message - The message to send.
    • publish

      void publish(String channelID, Map<String,String> message, MTUserHeader receiver)
      Publishes a message on a channel, but only to a given user.
      Parameters:
      channelID - The ID of the channel.
      message - The message to send.
      receiver - The only user that will receive the event, given that this user actually have subscribed to this channel.
    • publish

      void publish(String channelID, String message)
      Publishes a string message on a channel.
      Parameters:
      channelID - The ID of the channel.
      message - The message to send.
    • publish

      void publish(String channelID, String message, MTUserHeader receiver)
      Publishes a string message on a channel, but only to a given user.
      Parameters:
      channelID - The ID of the channel.
      message - The message to send.
      receiver - The only user that will receive the event, given that this user actually have subscribed to this channel.
    • addMessageServiceListener

      void addMessageServiceListener(MessageServiceListener l)
      Adds a listener for changes to the service, such as users logging on/off.
      Parameters:
      l - The listener to add.
    • removeMessageServiceListener

      void removeMessageServiceListener(MessageServiceListener l)
      Removes a listener for changes to the service.
      Parameters:
      l - The listener to remove.
    • getUsers

      List<MTUserHeader> getUsers()
      Returns the currently logged on users.
      Returns:
      the currently logged on users.
    • getUser

      MTUserHeader getUser(String username)
      Gets a logged-on user by username.
      Parameters:
      username - The username of the user to return.
      Returns:
      The user with the given username.
    • me

      Returns the current user for the service.
      Returns:
      the current user for the service.
    • reconnect

      boolean reconnect()
      Tries to reconnect after loosing connection with the server.
      Returns:
      true if the service was able to reconnect, false otherwise.
    • dispose

      void dispose()
      Disposes the message service after it is no longer to be used.
    • isConnectionLost

      default boolean isConnectionLost()
      Returns true if the connection to the server is lost.
      Returns:
      true if the connection to the server is lost.