lightweight RPC support for the ws WebSocket server
0.0.6 - Upon disconnection, client now errors (cleans) all pending (client-initiated) callbacks.
For increased reliability, it is suggested to use the WebSocket protocol over encrypted connections only. Some proxy servers handle the unencrypted form of the protocol in a way which prevents it from operation. This is being addressed by "masking" in newer versions of the protocol, however too many older versions are already in the wild. See the second code example on this page on how to configure HTTPS support in your Node.
npm install express npm install ws npm install ws-rpc npm install ws-flash-client npm install policyfile
argsstands for multiple arguments, e.g.
arg1, arg2, arg3
var prod = processenvNODE_ENV === 'production';var app = ;var ws = ;var wss = server: app ;var wsflash = ;;app;// create/get a named room (channel)var myRoom = wss;// handle a new client connectionwss;// *handle message from a client without a callbackwss;// *handle message from a client with a callbackwss;// handle client disconnectionwss;// message all clients connected to the serverwss;// message all clients in a roommyRoom;// and don't forget this if you don't want your Node to crash on an error:// if the error comes from a client, the client is passed as second parameterwss;
Binary messages are not handled by the RPC extension, so you can handle them separately using the classic
For completeness, here's a guide on how to get a free 90-day SSL certificate:
Make a subdirectory in your project folder, e.g.:
Create a text file in that directory, e.g.:
www.yourdomain.com.txt, with the following contents (C= is the ISO 3166-1 Alpha-2 code of your country)
[ req ] distinguished_name=req_distinguished_name prompt=no
[ req_distinguished_name ] C=US ST=State or County or Shire L=City O=Company Name CN=yourdomain.com emailAddressfirstname.lastname@example.org
Execute the following commands (the second command is to remove password from your secret-key file):
openssl genrsa -des3 -out www.yourdomain.com.key.sec 2048 openssl rsa -in www.yourdomain.com.key.sec -out www.yourdomain.com.key rm www.yourdomain.com.key.sec openssl req -new -config www.yourdomain.com.txt -key www.yourdomain.com.key -out www.yourdomain.com.csr
Use the CSR file to request a certificate from a recognized issuer. (You can get a free 90-day free certificate from here. I'm not affiliated in any way with that site, but it worked for me. UPDATE: these certificates display a warning on Firefox: "Error code: sec_error_unknown_issuer".)
After verification through your domain's contact email address, you will obtain a CRT file from the issuer. Place it along with the KEY file you generated above in the
Then configure your server as follows:
var prod = processenvNODE_ENV === 'production';var app = ;var ws = ;var wsflash = ;;var httpsOptions =key: fscert: fs;var httpServer = http;var httpsServer = https;var wss = server: httpServer httpsServer ;app
In your HTML page (JADE syntax shown):
!!! 5 html head meta(http-equiv='X-UA-Compatible', content='IE=Edge,chrome=1') //if lt IE 8 script(src='http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.min.js') script(src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js') script(src='/ws-rpc-client.min.js') script(src='/ws-flash.min.js') script(src='/myclientcode.js') body #msg
Complete example of the
myclientcode.js with the Flash client shim support (for full usage and options please see ws-flash-client):
var WebSocketRPC = ;var ws = 'ws://' + windowlocationhost + '/';// ..and the same operations as above..
var ws = ;var wsc = 'ws://127.0.0.1:1144/';wsc;// etc.
Find the example app here, or installed in
cd example npm install node testws
Then load http://localhost:1144/ in your browsers, and/or open another console and
cd example node testws-server-client
├─┬ email@example.com │ ├─┬ firstname.lastname@example.org │ │ └── email@example.com │ ├── firstname.lastname@example.org │ ├── email@example.com │ └── firstname.lastname@example.org ├─┬ email@example.com │ ├── firstname.lastname@example.org │ └── email@example.com ├── firstname.lastname@example.org ├── email@example.com ├─┬ firstname.lastname@example.org │ ├── email@example.com │ ├── firstname.lastname@example.org │ ├── email@example.com │ └── firstname.lastname@example.org ├─┬ email@example.com │ ├── firstname.lastname@example.org │ └── email@example.com ├── firstname.lastname@example.org └── email@example.com
New BSD License.