| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
The following example attaches socket.io to a plain Node.JS HTTP server listening on port 3000.
var server = require('http').Server();
var io = require('socket.io')(server);
io.on('connection', function(socket){
socket.on('event', function(data){});
socket.on('disconnect', function(){});
});
server.listen(3000);var io = require('socket.io')();
io.on('connection', function(socket){});
io.listen(3000);Starting with 3.0, express applications have become request handler functions that you pass to http or http Server instances. You need to pass the Server to socket.io, and not the express application function.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function(){ // … });
server.listen(3000);Like Express.JS, Koa works by exposing an application as a request handler function, but only by calling the callback method.
var app = require('koa')();
var server = require('http').Server(app.callback());
var io = require('socket.io')(server);
io.on('connection', function(){ // … });
server.listen(3000);Exposed by require('socket.io').
Creates a new Server. Works with and without new:
var io = require('socket.io')();
// or
var Server = require('socket.io');
var io = new Server();Optionally, the first or second argument (see below) of the Server constructor can be an options object.
The following options are supported:
The same options passed to socket.io are always passed to the engine.io Server that gets created. See engine.io options as reference.
Creates a new Server and attaches it to the given srv. Optionally opts can be passed.
Binds socket.io to a new http.Server that listens on port.
If v is true the attached server (see Server#attach) will serve the client files. Defaults to true.
This method has no effect after attach is called.
// pass a server and the `serveClient` option
var io = require('socket.io')(http, { serveClient: false });
// or pass no server and then you can call the method
var io = require('socket.io')();
io.serveClient(false);
io.attach(http);If no arguments are supplied this method returns the current value.
Sets the path v under which engine.io and the static files will be served. Defaults to /socket.io.
If no arguments are supplied this method returns the current value.
Sets the adapter v. Defaults to an instance of the Adapter that ships with socket.io which is memory based (see below).
If no arguments are supplied this method returns the current value.
The default (/) namespace.
Attaches the Server to an engine.io instance on srv with the supplied opts (optionally).
Attaches the Server to an engine.io instance that is bound to port with the given opts (optionally).
Synonym of Server#attach.
Advanced use only. Binds the server to a specific engine.io Server (or compatible API) instance.
Advanced use only. Creates a new socket.io client from the incoming engine.io (or compatible API) socket.
Initializes and retrieves the given Namespace by its pathname identifier nsp.
If the namespace was already initialized it returns it right away.
Emits an event to all connected clients. The following two are equivalent:
var io = require('socket.io')();
io.sockets.emit('an event sent to all connected clients');
io.emit('an event sent to all connected clients');For other available methods, see Namespace below.
See Namespace#use below.
Represents a pool of sockets connected under a given scope identified by a pathname (eg: /chat).
By default the client always connects to /.
connection / connect. Fired upon a connection.
Parameters:
The namespace identifier property.
Hash of Socket objects that are connected to this namespace indexed by id.
Registers a middleware, which is a function that gets executed for every incoming Socket and receives as parameter the socket and a function to optionally defer execution to the next registered middleware.
var io = require('socket.io')();
io.use(function(socket, next){
if (socket.request.headers.cookie) return next();
done(new Error('Authentication error'));
});
Errors passed to middleware callbacks are sent as special error packets to clients.
A Socket is the fundamental class for interacting with browser clients. A Socket belongs to a certain Namespace (by default /) and uses an underlying Client to communicate.
A list of strings identifying the rooms this socket is in.
A reference to the underlying Client object.
A reference to the underyling Client transport connection (engine.io Socket object).
A getter proxy that returns the reference to the request that originated the underlying engine.io Client. Useful for accessing request headers such as Cookie or User-Agent.
The Client class represents an incoming transport (engine.io) connection. A Client can be associated with many multiplexed Socket that belong to different Namespaces.
A reference to the underlying engine.io Socket connection.
A getter proxy that returns the reference to the request that originated the engine.io connection. Useful for accessing request headers such as Cookie or User-Agent.
The Adapter is in charge of keeping track of what rooms each socket is connected to and passing messages to them.
By default the Adapter is memory based. In order to pass messages across multiple processes, make sure to use an appropriate adapter. (configurable through Server#adapter).
MIT
| Back | FazBrowse Home | New Git URL |