-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67fe472
commit ed81cda
Showing
8 changed files
with
627 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
}); | ||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
Oops, something went wrong.