Hey there, this is an early release for Node Knockout, there'll be full documentation coming really soon, if anything's missing, try the source code.
# Node WebSocket Server is a near specification compliant implementation of the server-side WebSocket Protocol. It is built on top of Node.js as a third-party module, and is designed to support various versions of the WebSocket protocol – which is currently still an IETF Draft.
# The WebSockets protocol is a way of the browser providing a bi-directional, full-duplex communication channel which is accessible through the DOM.
# Node WebSocket Server can be easily installed via NPM or cloned from github. This module requires Node to be compiled with SSL support (which is by default enabled, if it's available), the reason for the dependency on SSL being available in Node.js is to enable Draft 76 handshaking and Secure WebSocket connections.
# For the latest stable build:
npm install websocket-server
# Or, alternatively, you can run with the latest bleeding edge build with:
npm install websocket-server@latest
# Installing Node WebSocket Server as a git submodule is a recommended route for installing if you plan on deploying your application and don't want the hassle of having to check that both production and development environments are running the same module versions. Another benefit of installing as a submodule, is that the websocket server is then packaged with your code.
# The installation is as follows:
// Assumption that you are in a working git repository:
$ mkdir vendor $ cd vendor $ git submodule add git://github.com/miksago/node-websocket-server.git websocket-server Initialized empty Git repository in ./websocket-server/.git/// ...repository gets cloned...
$ git commit -m "Adding websocket-server submodule"
# Later on, in order to update your projects copy of Node WebSocket Server, simply do:
$ git submodule update
#
For your collaborators, they can simply do a git pull
then a git submodule init
then a git submodule update
. You can learn more about Git Submodules from Chapter 6.6 of the Pro Git book, which you can read online.
# In order to contribute to this project, I ask that you follow these steps:
# The API to Node WebSocket Server is fairly simple, and stays consistent with the Node.js API's you may already be familiar with. Firstly, there are three main parts to the server, these are: The constructor, the connection instance, and the connection manager. Each of these parts interact with each other, and end up providing a clean API with which you write your server-side logic.
# An example server, that simply echoes anything that a client sends to all other connections, looks like the following:
var
ws=
require("websocket-server"
);var
server=
ws.createServer(); server.addListener("connection"
,function
(connection){ connection.addListener("message"
,function
(msg){ server.send
(msg); }); }); server.listen
(8080
);
#
So what does the above code actually do? First, we need to require in the websocket-server library — If you have installed this module via NPM then you can require it exactly like the above does. Next, we create a server instance, by default, this server will automatically choose which version of the protocol to use when communicating with the client and it will also act as a standard http.Server
instance.
#
After we have a instance of ws.Server
, which ws.createServer
returns, we can proceed to add event listeners to the various events it emits. We use the same API as that which node's Event.EventEmitter provides to do so. In this case, we only bind to one event on the server, the Connection event. Each time a user successfully connects to the server the connection is emitted. We then watch for the message
event on the emitted connection, this event occurs every time a user sends a message to the server.
# The API to Node WebSocket Server is split into three main parts:
ws.createServer( [options] );
options
.ws.Server( [options] );
options
is an object of options to pass to the server, these are as follows:stdout
.Type: Boolean
Type: String
Values: "auto", "draft76", "draft75"
Default: "auto"
Type: Boolean, Object, Constructor
Type: http.Server
Type: String, String[]
Note: This is not fully implemented
Type: String, String[]
Note: This is not fully implemented
Throughout this section, the variable server
is an instant of a ws.Server.
server.send( client_id, message )
message
to the client with id
of client_id
.client_id: Integer
message: String
server.broadcast( message )
message
to all connected clients.message: String
server.listen( port, [host] )
server.setSecure( credentials )
credentials
. Requires the client to connect using wss://
protocol, instead of the standard ws://
.credentials: crypto.Credentials
server.close()
The following methods are inherited from Events.eventEmitter.
server.addListener( event, callback )
event: String
callback: Function
server.removeListener( event, callback )
event: String
callback: Function
server.removeAllListeners( event )
event: String
A Server instance can emit various events during it's life cycle. The WebSocket Server will also emit each of the http.Server events, except "connection" and "close" which are used by the WebSocket Server. These events are described as follows:
client: Connection
client: Connection
#
Each time a client connects to the server
, a new connection instance is created and registered with the connection manager.
connection.id
connection.state
connection.version
connection.headers
connection.storage
connection.send( data )
data
from the current connection to all other connections on the server, including the sending connection. This method is aliased as connection.write
data: String
connection.broadcast( data )
data
from the current connection to all other connections on the server, excluding the sending connection.data: String
connection.close()
"close"
.connection.reject( [reason] )
"rejected"
event, then proceeds to close the connection using connection.close()
. The reason
is used only for logging purposes.reason: String
The following methods are inherited from Events.eventEmitter.
connection.addListener( event, callback )
event: String
callback: Function
connection.removeListener( event, callback )
event: String
callback: Function
connection.removeAllListeners( event )
event: String
Each connection comes with the following events that may be emitted through out it's life-cycle.
# ...
server.manager.length
server.manager.find( id, callback, [thisArg] )
id
. If a match is found, the call
property of callback
is called with thisArg
as the value of this, with the only argument being the pointer to the Connection Instance that was matched.server.manager.forEach( callback, [thisArg] )
server.manager.map( callback, [thisArg] )
server.manager.filter( callback, [thisArg] )