Coder Social home page Coder Social logo

gonzrb / socket.io Goto Github PK

View Code? Open in Web Editor NEW

This project forked from socketio/socket.io

0.0 1.0 0.0 80 KB

Realtime application framework for Node.JS, with HTML5 WebSockets and cross-browser fallbacks support.

Home Page: http://socket.io

License: MIT License

socket.io's Introduction

This Readme corresponds to the upcoming 1.0 release. Please refer to http://socket.io for the current 0.9.x documentation.


socket.io

Build Status

How to use

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);

In conjunction with Express

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);

API

Server

Exposed by require('socket.io').

Server()

Creates a new Server. Works with and without new:

var io = require('socket.io')();
// or
var Server = require('socket.io');
var io = new Server();

Server(opts:Object)

Optionally, the first or second argument (see below) of the Server constructor can be an options object.

The following options are supported:

  • static sets the value for Server#static()
  • path sets the value for Server#path()

Options are always passed to the engine.io Server that gets created.

Server(srv:http#Server, opts:Object)

Creates a new Server and attaches it to the given srv. Optionally opts can be passed.

Server(port:Number, opts:Object)

Binds socket.io to a new http.Server that listens on port.

Server#static(v:Boolean):Server

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 `static` option
var io = require('socket.io')(http, { static: false });

// or pass no server and then you can call the method
var io = require('socket.io')();
io.static(false);
io.attach(http);

If no arguments are supplied this method returns the current value.

Server#path(v:String):Server

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.

Server#adapter(v:Adapter):Server

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.

Server#sockets:Namespace

The default (/) namespace.

Server#attach(srv:http#Server, opts:Object):Server

Attaches the Server to an engine.io instance on srv with the supplied opts (optionally).

Server#attach(port:Number, opts:Object):Server

Attaches the Server to an engine.io instance that is bound to port with the given opts (optionally).

Server#bind(srv:engine#Server):Server

Advanced use only. Binds the server to a specific engine.io Server (or compatible API) instance.

Server#onconnection(socket:engine#Socket):Server

Advanced use only. Creates a new socket.io client from the incoming engine.io (or compatible API) socket.

Server#of(nsp:String):Namespace

Initializes and retrieves the given Namespace by its pathname identifier nsp.

If the namespace was already initialized it returns it right away.

Server#emit

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.

Namespace

Represents a pool of sockets connected under a given scope identified by a pathname (eg: /chat).

By default the client always connects to /.

Events

  • connection / connect. Fired upon a connection.

    Parameters:

    • Socket the incoming socket.

Namespace#name:String

The namespace identifier property.

Namespace#connected:Object

Hash of Socket objects that are connected to this namespace indexed by id.

Socket

A Socket is the fundamental class for interacting with browser clients.

Socket#rooms:Array

A list of strings identifying the rooms this socket is in.

Client

The Client class represents an incoming transport (engine.io) connection. A Client can be associated with many multiplexed Socket that belong to different Namespaces.

Adapter

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).

License

MIT

socket.io's People

Contributors

3rd-eden avatar achesser avatar ad7six avatar bwillard avatar chees avatar christopherobin avatar coreh avatar crickeys avatar doozr avatar dshaw avatar dvv avatar dylang avatar einaros avatar ericz avatar felixge avatar fjakobs avatar gavinuhma avatar gicodewarrior avatar itaisatati avatar jmatthewsr avatar martinthomson avatar matthewmueller avatar mattrobenolt avatar mbrevoort avatar mpreziuso avatar rauchg avatar shapeshed avatar tj avatar xaroth8088 avatar znarkus avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.