Socket.MVC
Socket.MVC extends the usability of Socket.io in an MVC environment. The current version of Socket.MVC has only been tested in an application using Express. Future releases will be tested using the different frameworks that Socket.io supports. Socket.MVC is designed for versions 0.9x, but in future releases versions 1.0 will be supported. The only thing Socket.MVC wants to do is give you the freedom to emit from any file in your application at any time.
Install
First you will need a version of Socket.io. The fastest way to accomplish this is using NPM.
npm install socket.io
Next use NPM to install Socket.MVC
npm install socket.mvc
How to use
app.js
var express = ;var routes = ;var app = moduleexports = express;var io = ;var socketMVC = ; //Set socket.io configuration here iosockets;
routes/socket.js
module { //You can declare all of your socket listeners in this file, but it's not required socket;};
controllers/index.js
var socketMVC = ; /*Login logic*/socketMVC;
How to get the most out of Socket.MVC
In the example above you see 3 arguments being passed into the init function. The first is Socket.io, is the socket returned from the Socket.io connection, and the third is the options object for Socket.MVC. You do not have to place all of your socket listeners in a separate JS file, but it will make your life easier. The best feature of Socket.MVC is that you can simply require the module, and then just send an emit, or a broadcast or any other information through the websocket inside any function in any file of the application.
API
Init
Init takes 3 different arguments.
socketMVC.init('socket.io', 'socket', 'config')
Socket.io
This is simply avar
that is equal torequire('socket.io')
Socket
This is the socket that is returned from Socket.io after a connection is madeConfig
This is an obj that you can pass to configure your Socket.MVC experience
The Config
object is structured as the following:
debug: boolean //Default to true, but in production make sure you change it to false filePath: string or array //This can be an array of file paths, or a single file path to register Socket.io listeners
Emit
Emit is the bread and butter of Socket.MVC, because you can now send data via Websockets from any file in your application. Socket.MVC will queue events that have taken place prior to a websocket connection, meaning, if you start up your application and no one has connected to it (for the first time only), but events are running SocketMVC.emit
, the events will be thrown into a queue, and once a connection is made the queued events will be sent to appropriate connections.
socketMVC;
Broadcast
Broadcast will send data to every connection except the connection that triggered the event. This will also take advantage of the event queuing system, and will broadcast once a connection is made.
socketMVC;
Everyone
Everyone is an API that allows you to broadcast an event to every socket (including the socket that triggered the event). This API also takes advantage of the event queuing system.
socketMVC;
On
The on event registers a listener for Socket.io, but it is preferred that you use this method inside a socket listener file. If you choose to use the API it will still work, but what takes place is that Socket.MVC will store your on
events in a queue, and when a client connects to Socket.io the listeners will then be registered asynchronously.
Socket.io API's
Since Socket.MVC is just a wrapping mechanism for Socket.io, all of the same API's can be used using the Socket.MVC module. Please see a list of all of the API's available by visiting the Socket.io Github page, or http://socket.io (depending on your version)