ForgeTotal.js 5
en

Total.js 5

Build a WebSocket chat

Total.js 5 · total5@0.0.19 websocketrealtime Executed against Total.js 5

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.

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 });
	});
}