# Build a WebSocket chat

> Declare a SOCKET route, listen to messages and broadcast them to every connected client.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: en
Content-SHA256: cf99e7bc7621eedaf3fef99ac01abd403fb18681dbe94220c553c2ec6d39bc58
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

A WebSocket endpoint is declared with `ROUTE('SOCKET /path/', fn)`. Inside the handler `this` is the WebSocket controller: use `self.on('message', ...)` to receive data and `self.send(...)` to broadcast to all connected clients. `client.send(...)` answers only the sender. Objects are sent and received as JSON by default.

```js controllers/chat.js
exports.install = function() {
	ROUTE('SOCKET /chat/', socket);
};

function socket() {

	var self = this;

	self.on('message', function(client, message) {
		self.send({ type: 'message', text: message.text });
	});
}
```
