To implement our server, we need to implement the three parts of our system described in the previous example. We will first create our data source, then our WebSocket server, and finally our services and their tests.
As we have no interest in keep the history of messages nor in implementing a login for our server (the users are going to just type a name and this name will be used), we can use a simple observable to implement our DataSource; this DataSource will contain only the messages sent from user to user and broadcast messages.
Our DataSource must have two methods: the first to push new messages, and the second to listen to incoming messages. We can use a RxJS
Subject to implement this behavior as it lets us push data to our observable. We could also user an observable using the
create() method to create it from an arbitrary source, but the code with a
Subject will be a lot easier to read and to understand:
let Rx = require('rx'); let messagesSubject...