         reject new errors.PubSubError message, null
 
     .then (response) -> response.uuid
+
+  # Publish a message to a channel expecting an acknowledgement on success
+  publishWithAck: (channel, message) ->
+    @publish channel, message, true
+    .then (seq) =>
+      @outstanding.set seq,
+        resolve: resolve
+        reject: reject
     
   # Publish a message to a channel.
-  publish: (channel, message) ->
-    new P (resolve, reject) =>
-      if @sock?
-        seq = @sequence
-        @sequence += 1
+  publish: (channel, message, ack) ->
+    new P (resolve, reject) =>
+      if @sock?
+        ack = undefined if ack != true
+        seq = @sequence += 1
         
         record =
           seq: seq
           action: 'pub'
           chan: channel
           msg: message
+          ack: ack
+        
+        @sock.send JSON.stringify(record)
+        .then => resolve seq
+        .catch (error) ->
+          message = "Socket error while publishing message:"
+          logger.error message, error
+          reject new errors.PubSubError message, error 
+        
+      else
+        message = "Could not publish a message as the socket is disconnected."
+        logger.warn message
+        reject new errors.PubSubError message, null
+
+  # Subscribe to a channel.
+  subscribe: (channel, handler) ->
+    if typeof handler == 'function'
+      @handlers[channel] = handler
+
+    new P (resolve, reject) =>
+      if @sock?
+        seq = @sequence += 1
+        
+        record =
+          seq: seq
+          action: 'subscribe'
+          channel: channel
         
         @sock.send JSON.stringify(record)
         .then =>

            resolve: resolve
            reject: reject
        .catch (error) ->
          message = "Socket error while publishing message:"
          logger.error message, error
          reject new errors.PubSubError message, error 
        
      else
        message = "Could not publish a message as the socket is currently disconnected."
        logger.warn message
        reject new errors.PubSubError message, null

  # Subscribe to a channel.
  subscribe: (channel, handler) ->
    if typeof handler == 'function'
      @handlers[channel] = handler

    new P (resolve, reject) =>
      if @sock?
        seq = @sequence
        @sequence += 1
        
        record =
          seq: seq
          action: 'subscribe'
          channel: channel
        
        @sock.send JSON.stringify(record)

            resolve: resolve
            reject: reject
        .catch (error) ->
          message = "Socket error while unsubscribing from channel:"
          logger.error message, error
          reject new errors.PubSubError message, error 
        
      else
        message = "Could not unsubscribe from channel as the socket is currently disconnected."
        logger.warn message
        reject new errors.PubSubError message, null

    .then (response) -> response.channels

  # Unsubscribe from a channel.
  unsubscribeAll: ->
    @handlers = {}

    new P (resolve, reject) =>
      if @sock?
        seq = @sequence += 1
        
        record =
          seq: seq
          action: 'unsubscribe-all'
        
        @sock.send JSON.stringify(record)
        .then =>
