| CARVIEW |
- Source
- Commits
- Network (64)
- Issues (15)
- Graphs
-
Branch:
master
click here to add a description
click here to add a homepage
| name | age | message | |
|---|---|---|---|
| |
.gitmodules | Sun Oct 03 14:24:16 -0700 2010 | Moved dependencies into support/ from tests/sup... [guille] |
| |
Makefile | Sun Oct 03 14:44:17 -0700 2010 | Adapting websocket client api to ev.data [guille] |
| |
README.md | Tue Sep 28 04:09:47 -0700 2010 | Node required version [guille] |
| |
example/ | Thu Sep 09 17:54:25 -0700 2010 | Socket.IO-node now serves the client out of the... [guille] |
| |
index.js | Tue Aug 24 17:18:22 -0700 2010 | Make it possible to just require 'socket.io' [guille] |
| |
lib/ | Wed Oct 06 01:00:07 -0700 2010 | Updated the flash socket with error detection, ... [3rd-Eden] |
| |
package.json | Thu Sep 23 03:34:31 -0700 2010 | add package.json for npm [maccman] |
| |
support/ | Sun Oct 03 14:24:16 -0700 2010 | Moved dependencies into support/ from tests/sup... [guille] |
| |
tests/ | Mon Oct 04 12:41:48 -0700 2010 | Clearing heartbeat interval upon closing the co... [guille] |
Socket.IO Server: Sockets for the rest of us
The Socket.IO server provides seamless supports for a variety of transports intended for realtime communication
- WebSocket
- WebSocket over Flash (+ XML security policy support)
- XHR Polling
- XHR Multipart Streaming
- Forever Iframe
- JSONP Polling (for cross domain)
Requirements
- Node v0.1.103+
- Socket.IO client to connect from the browser
How to use
To run the demo:
git clone git://github.com/LearnBoost/Socket.IO-node.git socket.io-node --recursive
cd socket.io-node/example/
sudo node server.js
and point your browser to https://localhost:8080. In addition to 8080, if the transport flashsocket is enabled, a server will be initialized to listen to requests on the port 843.
Implementing it on your project
Socket.IO is designed not to take over an entire port or Node http.Server instance. This means that if you choose your HTTP server to listen on the port 80, socket.io can intercept requests directed to it and the normal requests will still be served.
By default, the server will intercept requests that contain socket.io in the path / resource part of the URI. You can change this (look at the available options below).
var http = require('http'),
io = require('./path/to/socket.io'),
server = http.createServer(function(req, res){
// your normal server code
res.writeHeader(200, {'Content-Type': 'text/html'});
res.writeBody('<h1>Hello world</h1>');
res.finish();
});
// socket.io, I choose you
var socket = io.listen(server);
socket.on('connection', function(client){
// new client is here!
client.on('message', function(){ … })
client.on('disconnect', function(){ … })
});
On the client side:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = new io.Socket();
socket.on('connect', function(){ … })
socket.on('message', function(){ … })
socket.on('disconnect', function(){ … })
</script>
The client side files will be served automatically by Socket.IO-node.
Notes
IMPORTANT! When checking out the git repo, make sure to include the submodules. One way to do it is:
git clone [repo] --recursive
Another, once cloned
git submodule update --init --recursive
Documentation
Listener
io.listen(<http.Server>, [options])
Returns: a Listener instance
Public Properties:
-
server
The instance of process.http.Server
-
options
The passed in options combined with the defaults
-
clients
An object of clients indexed by their session ids.
Methods:
-
addListener(event, λ)
Adds a listener for the specified event. Optionally, you can pass it as an option to
io.listen, prefixed byon. For example:onClientConnect: function(){} -
removeListener(event, λ)
Remove a listener from the listener array for the specified event.
-
broadcast(message, [except])
Broadcasts a message to all clients. There's an optional second argument which is an array of session ids or a single session id to avoid broadcasting to.
Options:
-
resource
socket.ioThe resource is what allows the
socket.ioserver to identify incoming connections bysocket.ioclients. Make sure they're in sync. -
transports
['websocket', 'server-events', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling']A list of the accepted transports.
-
transportOptions
An object of options to pass to each transport. For example
{ websocket: { closeTimeout: 8000 }} -
log
ƒ(){ sys.log }The logging function. Defaults to outputting to stdout through
sys.log
Events:
-
clientConnect(client)
Fired when a client is connected. Receives the Client instance as parameter
-
clientMessage(message, client)
Fired when a message from a client is received. Receives the message and Client instance as parameter
-
clientDisconnect(client)
Fired when a client is disconnected. Receives the Client instance as parameter
Important note: this in the event listener refers to the Listener instance.
Client
Client(listener, req, res)
Public Properties:
-
listener
The
Listenerinstance this client belongs to. -
connected
Whether the client is connected
-
connections
Number of times the client connected
Methods:
-
send(message)
Sends a message to the client
-
broadcast(message)
Sends a message to all other clients. Equivalent to Listener::broadcast(message, client.sessionId)
Protocol
One of the design goals is that you should be able to implement whatever protocol you desire without Socket.IO getting in the way. Socket.IO has a minimal, unobtrusive protocol layer. It consists of two parts:
-
Connection handshake
This is required to simulate a full duplex socket with transports such as XHR Polling or Server-sent Events (which is a "one-way socket"). The basic idea is that the first message received from the server will be a JSON object that contains a session id that will be used for further communication exchanged between the client and the server.
The concept of session also benefits naturally full-duplex WebSocket, in the event of an accidental disconnection and a quick reconnection. Messages that the server intends to deliver to the client are cached temporarily until the reconnection.
The implementation of reconnection logic (potentially with retries) is left for the user. By default, transports that are keep-alive or open all the time (like WebSocket) have a timeout of 0 if a disconnection is detected.
-
Message batching
In order to optimize the resources, messages are buffered. In the event of the server trying to send multiple messages while the client is temporarily disconnected (eg: xhr polling), messages are stacked, then encoded in a lightweight way and sent to the client whenever he becomes available.
Despite this extra layer, your messages are delivered unaltered to the different event listeners. You can JSON.stringify() objects, send XML, or maybe plain text.
Credits
License
(The MIT License)
Copyright (c) 2010 LearnBoost <dev@learnboost.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- © 2010 GitHub Inc. All rights reserved.
- Terms of Service
- Privacy
- Security
Keyboard Shortcuts
Site wide shortcuts
- s
- Focus site search
- ?
- Bring up this help dialog
Commit list
- j
- Move selected down
- k
- Move selected up
- t
- Open tree
- p
- Open parent
- c or o or enter
- Open commit
Pull request list
- j
- Move selected down
- k
- Move selected up
- o or enter
- Open issue
Issues
- j
- Move selected down
- k
- Move selected up
- x
- Toggle select target
- o or enter
- Open issue
- I
- Mark selected as read
- U
- Mark selected as unread
- e
- Close selected
- y
- Remove selected from view
- c
- Create issue
- l
- Create label
- i
- Back to inbox
- u
- Back to issues
- /
- Focus issues search
Network Graph
- ← or h
- Scroll left
- → or l
- Scroll right
- ↑ or k
- Scroll up
- ↓ or j
- Scroll down
- t
- Toggle visibility of head labels
- shift ← or shift h
- Scroll all the way left
- shift → or shift l
- Scroll all the way right
- shift ↑ or shift k
- Scroll all the way up
- shift ↓ or shift j
- Scroll all the way down