Skip to content

Commit

Permalink
merge branch socket.io
Browse files Browse the repository at this point in the history
  • Loading branch information
ianjennings committed Nov 18, 2016
1 parent 67fe472 commit ed81cda
Show file tree
Hide file tree
Showing 8 changed files with 627 additions and 4 deletions.
35 changes: 35 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
extends: airbnb/base
# plugins:
# - mocha
globals:
describe: true
it: true
beforeEach: true
afterEach: true
Promise: true
rules:
indent:
- error
- 4
max-len:
- error
- 120
- ignoreComments: true
- ignoreUrls: true
global-require: 0
vars-on-top: 0
padded-blocks: 0
no-console: 0
prefer-const: 0
prefer-template: 0
comma-dangle: 0
consistent-return: 0
no-param-reassign: 0
no-underscore-dangle: 0
no-unused-vars:
- warn
- varsIgnorePattern: "process"
import/no-unresolved:
- "error"
- ignore:
- state
126 changes: 125 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,125 @@
# realtimejs
# rltm.js

Universal API for integrating realtime providers.

## Setup

```js
let rltm = require('rltm');
```

### Config

```sh
npm install rltm --save
```

#### PubNub

```js
let agent = rltm('pubnub', {
publishKey: 'YOUR_PUBNUB_PUBLISH_KEY',
subscribeKey: 'YOUR_PUBNUB_SUBSCRIBE_KEY'
});
```

#### Socket.io

```js
let agent = rltm('socketio', {
endpoint: 'http://localhost:8000'
});
```

See socket.io-server.js for an example socket.io implementation.

## Usage

### Identify User

```js
let agent = rltm('socketio', {
endpoint: 'http://localhost:8000',
uuid: 'MY_USER_ID',
state: {admin: true}
});
```

### Join a room

```js
room = agent.join('some-room');
```

```js
room.on('ready', () => {
console.log('connected to room');
});
```

### Publish Subscribe

```js
room.on('message', (uuid, data) => {
console.log('message received from uuid', uuid, 'with data', data);
});

room.publish({hello: world});
```

### Set User

```js
room.on('state', function(uuid, state) {
console.log('user with uuid', uuid, 'was given state', state);
});

socket.setState({idle: true});
```

### Who's in room

```js
room.on('join', (uuid, state) => {
console.log('user with uuid', uuid, 'joined with state', state);
});
```

```js
room.on('leave', (uuid) => {
console.log('user with uuid', uuid, 'has left');
});
```

```js
room.hereNow(function(users) {
console.log('users are online', users);
});
```

### Get Old Messages

```js
room.history(function(history) {
console.log('got array of all messages in channel', history);
});
```

## Test

Tests are run with mocha and chai.

```sh
npm install mocha -g
npm install chai -g
```

Set environment variable ```AGENT``` to test service.

```sh
env AGENT=pubnub mocha
```

```sh
env AGENT=socketio mocha
```
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rltm",
"version": "0.0.4",
"version": "0.0.6",
"description": "abstraction for realtime frameworks",
"main": "src/index.js",
"scripts": {
Expand All @@ -25,11 +25,14 @@
"homepage": "https://github.com/pubnub/rltm#readme",
"devDependencies": {
"browserify": "^13.1.0",
"chai": "^3.5.0",
"gulp": "^3.9.1",
"vinyl-source-stream": "^1.1.0"
},
"dependencies": {
"browserify": "^13.1.0",
"pubnub": "^4.0.13"
"pubnub": "^4.0.13",
"socket.io": "^1.5.1",
"socket.io-client": "^1.5.1"
}
}
72 changes: 72 additions & 0 deletions socket.io-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
console.log('starting socket io server')

var io = require('socket.io')(8000);

var users = {};

let channel = 'test-channel';

let room = io.of(channel);

let messageHistory = [];

room.on('connection', function (socket) {

// when the client emits 'subscribe', this listens and executes
socket.on('start', function (uuid, state) {

// statetore user in object
users[uuid] = state;

socket.uuid = uuid;
socket.state = state;

// echo globally (all clients) that a person has connected
room.emit('join', uuid, state);

});

socket.on('setState', function (uuid, state) {

users[uuid] = state;

room.emit('state', uuid, state);

});

// when the client emits 'add user', this listens and executes
socket.on('publish', function (uuid, data, fn) {

io.of(channel).emit('message', uuid, data);

messageHistory.unshift({uuid: uuid, data: data});
if(messageHistory.length > 100) {
messageHistory.pop();
}

});

socket.on('whosonline', function (data, fn) {

// callback with user data
fn(users);

});

//
socket.on('history', function (data, fn) {
fn(messageHistory);
});

// when the user disconnects.. perform this
socket.on('disconnect', function () {

delete users[socket.uuid];

// echo globally that this client has left
socket.broadcast.emit('leave', socket.uuid);

});


});
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module.exports = require('pubnub');
"use strict";

module.exports = function(service, channel, config) {
return require(__dirname + '/services/' + service)(service, channel, config);
};
Loading

0 comments on commit ed81cda

Please sign in to comment.