 
     func appendMessage() {
         self.chatTableView.beginUpdates()
-        let index = IndexSet(integer: self.messages.count - 1)
+        let index = IndexSet(integer: self.chatTableView.numberOfRows)
         self.chatTableView.insertRows(at: index)
         self.chatTableView.endUpdates()
         self.chatTableView.scrollRowToVisible(self.chatTableView.numberOfRows - 1)

        chatHubConnection = HubConnection(url: URL(string:"http://localhost:5000/chat")!, query: "")
        chatHubConnection!.delegate = chatHubConnectionDelegate
        chatHubConnection!.on(method: "NewMessage", callback: {args in
            self.messages.append("\(args[0]!): \(args[1]!)")
            self.appendMessage()

        })

    @IBOutlet weak var msgTextField: NSTextField!
    @IBOutlet weak var chatTableView: NSTableView!

    private let dispatchQueue = DispatchQueue(label: "hubsample.queue.dispatcheueuq")

    var chatHubConnection: HubConnection?
    var chatHubConnectionDelegate: ChatHubConnectionDelegate?
    var messages: [String] = []
