Chat
This example demonstrates how to build a relatively simple chat application using the JSMachine framework. The example consists of a chat server and web client together with two addtional command line clients. The socket.io library is used to provide the communication transport mechanism.
Server
The server is built using two state machines, chatRoom and user. The chatRoom machine controls the server and is also used to serve the HTTP text for the web client. For each new connection, the chatRoom machine spawns a new instance of a user state machine to handle the connections life cycle. Once spawned, the user machine opens a websocket to communicate with its client going forward. When the user machine receives a chat message it notifies the chatRoom state machine by emitting a 'broadcast' factory event. The chatRoom machine then broadcasts the message out to all the connected clients.
$ ./bin/ignite examples/apps/chat1/server.js
Running examples/apps/chat1/server.js
info - socket.io started
debug - served static /socket.io.js
debug - client authorized
info - handshake authorized 1915469299226796763
debug - setting request GET /socket.io/1/websocket/1915469299226796763
debug - set heartbeat interval for client 1915469299226796763
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"connected"}
debug - websocket writing 5:::{"name":"statusUpdate","args":[1]}
debug - websocket received data packet 5:::{"name":"signIn","args":[{"name":"david"}]}
debug - websocket writing 5:::{"name":"signedIn"}
debug - websocket received data packet 5:::{"name":"chat","args":[{"text":"hello world"}]}
debug - websocket writing 5:::{"name":"chat","args":[{"text":"hello world","name":"david"}]}
debug - client authorized
_
See
- source: server.js
Web client
The web client is a relatively simple standalone webpage that can be used to interact with the chat server. As soon as the page is loaded it will start receiving any messages that are broadcasted from the server. To send a message a simple name based login process is necessary.
See
- source: client.html
- demo: chat.ignitejs.com
Command line client
The command line client allows a user to interact with the chat server from the command line. It uses the client package for socket.io found here. It can be run as follows
$ ./bin/ignite examples/apps/chat1/client.js
9 Aug 11:19:02 - Initializing client with transport "websocket"
9 Aug 11:19:02 - Client 43666363321244717 connected
Enter user name:
{ type: 'status', count: 1 }
Enter user name:
david
Enter chat message:
hello world!!
message { type: 'chat', text: 'hello world!!', name: 'david' }
exit
closing socket
9 Aug 11:19:16 - Client 43666363321244717 disconnected
$ _
See
- source: client.js
RSS Feed client
The RSS feed client used the state machine from the RSS Catalog Reader example to send chat messages to the chat server whenever new RSS feeds are received. It can be started in the usual way by using the ignite helper application as follows
$ ./bin/ignite examples/apps/chat1/rss_client.js
Running examples/apps/chat1/rss_client.js
9 Aug 11:37:26 - Initializing client with transport "websocket"
9 Aug 11:37:26 - Client 33892894349992275 connected
{ type: 'status', count: 1 }
_
See
- source: rss_client.js