Giter VIP home page Giter VIP logo

node-chitchat's Introduction

chitchat—Real-time chatting for the web

Currently we use SockJS as a transport, but I would like to extend this project to allow any system to be plugged in.

Server

Setup

Install the library through NPM, dyuh!

npm install chitchat

Example

var http = require('http'),
    sockjs = require('sockjs'),
    chitchat = require('chitchat');

/* Instantiate your chitchat server */
var chat = chitchat.createServer();

/* Setup authorize function */
chat.handleAuthorize(function (data, good, bad) {
    signobj.valid(data.access_token).then(function (data) {
        good(data.username, {meta_data...});
    }).fail(function (err) {
        bad({invalid: true});
    });
});

/* Handle message function */
chat.handleMessage(function (data, good, bad) {
});

/* Hook into your HTTP server */
var server = http.createServer();
chat.installHandlers(server, {prefix: '/chat'});
server.listen(9999, '0.0.0.0');

var chat = chitchat.createServer()

Create a new chat server.

chat.handleAuthorize(fn(data, goodFn, badFn))

Handle authorization

data

Data to validate from the front end.

goodFn({String} identifier, {Object} [meta_data])

We authorized the user, you must set an identifier for this user. You may also send some meta data to the front end for the user, perhaps a list of online users!

badFn({Object} [meta_data])

Could not authorize the user, you can pass meta data along to the front end to tell the user why.


chat.handleMessage(fn(data, goodFn, badFn))

Handle message sending

chat.message({Object} user, {Object} message)

Send a custom message to a user.

Client

Hook this into your front end chat UI!

chitchat.connect({...})

chitchat.disconnect()

var state = chitchat.state()

States:

  • connected
  • disconnected
  • reconnecting

You may also get a boolean value of each of these states with the following methods.

var cond = chitchat.isConnected()

var cond = chitchat.isDisconnected()

var cond = chitchat.isReconnecting()

chitchat.send({message: 'rwar'})


Events

chitchat.on('connect', fn())

Connected to the chat server

chitchat.on('disconnect', fn())

Disconnected from the chat server

chitchat.on('reconnect', fn())

Reconnecting to the chat server, with the reason why we disconnected.

chitchat.on('messages', fn(messages))

Received a list of chat messages!

node-chitchat's People

Contributors

munro avatar

Watchers

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