view release on metacpan or search on metacpan
socialcalc/third-party/Socket.IO-node/Readme.md view on Meta::CPAN
## How to Install
npm install socket.io
## How to use
First, require `socket.io`:
```js
var io = require('socket.io');
```
Next, attach it to a HTTP/HTTPS server. If you're using the fantastic `express`
web framework:
```js
var app = express.createServer();
, io = io.listen(app);
app.listen(80);
socialcalc/third-party/Socket.IO-node/Readme.md view on Meta::CPAN
## Short recipes
### Sending and receiving events.
Socket.IO allows you to emit and receive custom events.
Besides `connect`, `message` and `disconnect`, you can emit custom events:
```js
// note, io.listen(<port>) will create a http server for you
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
io.sockets.emit('this', { will: 'be received by everyone');
socket.on('private message', function (from, msg) {
console.log('I received a private message by ', from, ' saying ', msg);
});
socket.on('disconnect', function () {
sockets.emit('user disconnected');
socialcalc/third-party/Socket.IO-node/Readme.md view on Meta::CPAN
```
### Storing data associated to a client
Sometimes it's necessary to store data associated with a client that's
necessary for the duration of the session.
#### Server side
```js
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.on('set nickname', function (name) {
socket.set('nickname', name, function () { socket.emit('ready'); });
});
socket.on('msg', function () {
socket.get('nickname', function (name) {
console.log('Chat message by ', name);
});
socialcalc/third-party/Socket.IO-node/Readme.md view on Meta::CPAN
This has the benefit of `multiplexing` a single connection. Instead of
socket.io using two `WebSocket` connections, it'll use one.
The following example defines a socket that listens on '/chat' and one for
'/news':
#### Server side
```js
var io = require('socket.io').listen(80);
var chat = io
.of('/chat');
.on('connection', function (socket) {
socket.emit('a message', { that: 'only', '/chat': 'will get' });
chat.emit('a message', { everyone: 'in', '/chat': 'will get' });
});
var news = io
.of('/news');
socialcalc/third-party/Socket.IO-node/Readme.md view on Meta::CPAN
If a certain client is not ready to receive messages (because of network slowness
or other issues, or because he's connected through long polling and is in the
middle of a request-response cycle), if he doesn't receive ALL the tweets related
to bieber your application won't suffer.
In that case, you might want to send those messages as volatile messages.
#### Server side
```js
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
var tweets = setInterval(function () {
getBieberTweet(function (tweet) {
socket.volatile.emit('bieber tweet', tweet);
});
}, 100);
socket.on('disconnect', function () {
clearInterval(tweets);
socialcalc/third-party/Socket.IO-node/Readme.md view on Meta::CPAN
Sometimes, you might want to get a callback when the client confirmed the message
reception.
To do this, simply pass a function as the last parameter of `.send` or `.emit`.
What's more, when you use `.emit`, the acknowledgement is done by you, which
means you can also pass data along:
#### Server side
```js
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.on('ferret', function (name, fn) {
fn('woot');
});
});
```
#### Client side
socialcalc/third-party/Socket.IO-node/Readme.md view on Meta::CPAN
### Broadcasting messages
To broadcast, simply add a `broadcast` flag to `emit` and `send` method calls.
Broadcasting means sending a message to everyone else except for the socket
that starts it.
#### Server side
```js
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.broadcast.emit('user connected');
socket.broadcast.json.send({ a: 'message' });
});
```
### Rooms
Sometimes you want to put certain sockets in the same room, so that it's easy
to broadcast to all of them together.
Think of this as built-in channels for sockets. Sockets `join` and `leave`
rooms in each socket.
#### Server side
```js
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.join('justin bieber fans');
socket.broadcast.to('justin bieber fans').emit('new fan');
io.sockets.in('rammstein fans').emit('new non-fan');
});
```
### Using it just as a cross-browser WebSocket
If you just want the WebSocket semantics, you can do that too.
Simply leverage `send` and listen on the `message` event:
#### Server side
```js
var io = require('socket.io-node').listen(80);
io.sockets.on('connection', function (socket) {
socket.on('message', function () { });
socket.on('disconnect', function () { });
});
```
#### Client side
```html
socialcalc/third-party/Socket.IO-node/Readme.md view on Meta::CPAN
</script>
```
### Changing configuration
Configuration in socket.io is TJ-style:
#### Server side
```js
var io = require('socket.io-node').listen(80);
io.configure(function () {
io.set('transports', ['websocket', 'flashsocket', 'xhr-polling']);
});
io.configure('development', function () {
io.set('transports', ['websocket', 'xhr-polling']);
io.enable('log');
});
```
socialcalc/third-party/Socket.IO-node/example/server-ssl.js view on Meta::CPAN
/**
* Important note: this application is not suitable for benchmarks!
*/
var https = require('https')
, url = require('url')
, fs = require('fs')
, io = require('../')
, sys = require(process.binding('natives').util ? 'util' : 'sys')
, server;
server = https.createServer({
key: fs.readFileSync(__dirname + '/key.key')
, cert: fs.readFileSync(__dirname + '/cert.crt')
}, function(req, res){
// your normal server code
var path = url.parse(req.url).pathname;
switch (path){
case '/':
socialcalc/third-party/Socket.IO-node/example/server.js view on Meta::CPAN
/**
* Important note: this application is not suitable for benchmarks!
*/
var http = require('http')
, url = require('url')
, fs = require('fs')
, io = require('../')
, sys = require(process.binding('natives').util ? 'util' : 'sys')
, server;
server = http.createServer(function(req, res){
// your normal server code
var path = url.parse(req.url).pathname;
switch (path){
case '/':
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<h1>Welcome. Try the <a href="/chat.html">chat</a> example.</h1>');
res.end();
socialcalc/third-party/Socket.IO-node/lib/socket.io/client.js view on Meta::CPAN
var urlparse = require('url').parse
, OutgoingMessage = require('http').OutgoingMessage
, Stream = require('net').Stream
, options = require('./utils').options
, encode = require('./utils').encode
, decode = require('./utils').decode
, merge = require('./utils').merge
, util = require(process.binding('natives').util ? 'util' : 'sys');
var Client = module.exports = function(listener, req, res, options, head){
process.EventEmitter.call(this);
this.listener = listener;
this.options(merge({
timeout: 8000,
heartbeatInterval: 10000,
closeTimeout: 0
}, this.getOptions ? this.getOptions() : {}), options);
this.connections = 0;
socialcalc/third-party/Socket.IO-node/lib/socket.io/index.js view on Meta::CPAN
exports.listen = function(server, options){
return new exports.Listener(server, options);
};
/**
* Listener constructor
*
* @api public
*/
exports.Listener = require('./listener');
/**
* Version
*/
exports.version = '0.6.18';
socialcalc/third-party/Socket.IO-node/lib/socket.io/listener.js view on Meta::CPAN
var url = require('url')
, util = require(process.binding('natives').util ? 'util' : 'sys')
, fs = require('fs')
, options = require('./utils').options
, Client = require('./client')
, clientVersion = require('./../../support/socket.io-client/lib/io').io.version
, transports = {};
var Listener = module.exports = function(server, options){
process.EventEmitter.call(this);
var self = this;
this.server = server;
this.options({
origins: '*:*',
resource: 'socket.io',
flashPolicyServer: true,
socialcalc/third-party/Socket.IO-node/lib/socket.io/listener.js view on Meta::CPAN
this.server.addListener('upgrade', function(req, socket, head){
if (!self.check(req, socket, true, head)){
socket.end();
socket.destroy();
}
});
this.options.transports.forEach(function(name) {
if (!(name in transports))
transports[name] = require('./transports/' + name);
if ('init' in transports[name]) transports[name].init(self);
});
this.options.log('socket.io ready - accepting connections');
};
util.inherits(Listener, process.EventEmitter);
for (var i in options) Listener.prototype[i] = options[i];
Listener.prototype.broadcast = function(message, except){
socialcalc/third-party/Socket.IO-node/lib/socket.io/transports/flashsocket.js view on Meta::CPAN
var net = require('net')
, util = require(process.binding('natives').util ? 'util' : 'sys')
, WebSocket = require('./websocket')
, listeners = []
, netserver;
var Flashsocket = module.exports = function(){
WebSocket.apply(this, arguments);
};
util.inherits(Flashsocket, WebSocket);
Flashsocket.httpUpgrade = true;
socialcalc/third-party/Socket.IO-node/lib/socket.io/transports/htmlfile.js view on Meta::CPAN
var Client = require('../client')
, util = require(process.binding('natives').util ? 'util' : 'sys')
, qs = require('querystring');
var HTMLFile = module.exports = function(){
Client.apply(this, arguments);
};
util.inherits(HTMLFile, Client);
HTMLFile.prototype._onConnect = function(req, res){
var self = this, body = '';
switch (req.method){
socialcalc/third-party/Socket.IO-node/lib/socket.io/transports/jsonp-polling.js view on Meta::CPAN
var XHRPolling = require('./xhr-polling')
, util = require(process.binding('natives').util ? 'util' : 'sys');
JSONPPolling = module.exports = function(){
XHRPolling.apply(this, arguments);
};
util.inherits(JSONPPolling, XHRPolling);
JSONPPolling.prototype.getOptions = function(){
return {
timeout: null, // no heartbeats
socialcalc/third-party/Socket.IO-node/lib/socket.io/transports/websocket.js view on Meta::CPAN
var Client = require('../client')
, Stream = require('net').Stream
, EventEmitter = require('events').EventEmitter
, url = require('url')
, util = require(process.binding('natives').util ? 'util' : 'sys')
, crypto = require('crypto');
WebSocket = module.exports = function(){
Client.apply(this, arguments);
};
util.inherits(WebSocket, Client);
WebSocket.prototype._onConnect = function(req, socket){
var self = this
, headers = [];
socialcalc/third-party/Socket.IO-node/lib/socket.io/transports/xhr-multipart.js view on Meta::CPAN
var Client = require('../client')
, util = require(process.binding('natives').util ? 'util' : 'sys')
, qs = require('querystring');
var Multipart = module.exports = function(){
Client.apply(this, arguments);
};
util.inherits(Multipart, Client);
Multipart.prototype._onConnect = function(req, res){
var self = this, body = '', headers = {};
// https://developer.mozilla.org/En/HTTP_Access_Control
socialcalc/third-party/Socket.IO-node/lib/socket.io/transports/xhr-polling.js view on Meta::CPAN
var Client = require('../client')
, util = require(process.binding('natives').util ? 'util' : 'sys')
, qs = require('querystring');
var Polling = module.exports = function(){
Client.apply(this, arguments);
};
util.inherits(Polling, Client);
Polling.prototype.getOptions = function(){
return {
timeout: null, // no heartbeats
socialcalc/third-party/Socket.IO-node/support/socket.io-client/bin/build view on Meta::CPAN
* @author Guillermo Rauch <guillermo@learnboost.com>
* @license The MIT license.
* @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
*/
/*
* This file will help you take all the Socket.IO client files and build socket.io.js.
* You can later use Google Closure Compiler on it.
*/
var fs = require('fs'),
socket = require('../lib/io'),
jsp = require('../lib/vendor/uglifyjs/lib/parse-js'),
pro = require("../lib/vendor/uglifyjs/lib/process"),
ast,
files = [
'io.js',
'util.js',
'transport.js',
'transports/xhr.js',
'transports/websocket.js',
'transports/flashsocket.js',
'transports/htmlfile.js',
'transports/xhr-multipart.js',