Skip to content

Commit 0dbe650

Browse files
committed
first version
0 parents  commit 0dbe650

File tree

6 files changed

+187
-0
lines changed

6 files changed

+187
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
*~
3+
*.swp
4+
.tern-port
5+
npm-debug.log

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# solid-ws
2+
3+
WebSockets for Solid
4+
5+
## Usage
6+
7+
### Simple way
8+
9+
```javascript
10+
11+
var ldnode = require('ldnode')
12+
13+
var server = ldnode.createServer({live: true})
14+
15+
server.listen(port, function () {
16+
console.log('Solid server started')
17+
})
18+
```
19+
20+
### Short way
21+
22+
```javascript
23+
var SolidWs = require('solid-ws')
24+
var ldnode = require('ldnode')
25+
26+
var server = ldnode.createServer()
27+
solidWs(server)
28+
29+
server.listen(port, function () {
30+
console.log('Solid server started')
31+
})
32+
```
33+
34+
### Long way
35+
36+
```javascript
37+
var SolidWs = require('solid-ws')
38+
var ldnode = require('ldnode')
39+
var express = require('express')
40+
var https = require('https')
41+
42+
var app = express()
43+
app.use('/databox', ldnode())
44+
var server = https.createServer({/* your settings*/}, app)
45+
46+
server.listen(port, function () {
47+
console.log('Solid server started')
48+
})
49+
50+
// Attach WS to solid
51+
solidWs(server)
52+
```

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var SolidWs = require('./server')
2+
3+
module.exports = function attachToServer (server, app, opts) {
4+
var solidWs = new SolidWs(server, opts)
5+
if (app) server.on('request', app)
6+
return solidWs
7+
}

lib/in-memory.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = InMemory
2+
3+
var uuid = require('node-uuid')
4+
5+
function InMemory (opts) {
6+
opts = opts || {}
7+
this.uris = opts.uris || {}
8+
this.subscribers = opts.subscribers || {}
9+
}
10+
11+
InMemory.prototype.subscribe = function (uri, client, callback) {
12+
var self = this
13+
this.uris[uri] = true
14+
15+
if (!this.subscribers[uri]) {
16+
this.subscribers[uri] = {}
17+
}
18+
19+
client.uuid = uuid.v1()
20+
this.subscribers[uri][client.uuid] = client
21+
22+
client.on('close', function () {
23+
delete self.subscribers[uri][client.uuid]
24+
})
25+
26+
return callback(null, client.uuid)
27+
}
28+
29+
InMemory.prototype.get = function (uri, callback) {
30+
return callback(null, this.subscribers)
31+
}

lib/server.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
var WebSocketServer = require('ws').Server
2+
var debug = require('debug')('solid:subscription')
3+
var InMemory = require('./in-memory')
4+
var parallel = require('run-parallel')
5+
6+
module.exports = WsServer
7+
8+
function WsServer (server, opts) {
9+
var self = this
10+
11+
opts = opts || {}
12+
this.suffix = opts.suffix || '.changes'
13+
this.store = opts.store || new InMemory(opts)
14+
15+
// Starting WSS server
16+
var wss = new WebSocketServer({
17+
server: server,
18+
clientTracking: false,
19+
path: opts.path
20+
})
21+
22+
// Handling a single connection
23+
wss.on('connection', function (client) {
24+
debug('New connection')
25+
// var location = url.parse(client.upgradeReq.url, true)
26+
27+
// Handling messages
28+
client.on('message', function (message) {
29+
debug('New message: ' + message)
30+
31+
if (!message || typeof message !== 'string') {
32+
return
33+
}
34+
35+
var tuple = message.split(' ')
36+
37+
// Only accept 'sub http://example.tld/hello'
38+
if (tuple.length < 2 || tuple[0] !== 'sub') {
39+
return
40+
}
41+
42+
self.store.subscribe(tuple[1], client, function (err, uuid) {
43+
if (err) {
44+
// TODO Should return an error
45+
return
46+
}
47+
48+
client.send('ack ' + uuid)
49+
})
50+
})
51+
52+
// Respond to ping
53+
client.on('ping', function () {
54+
client.pong()
55+
})
56+
})
57+
}
58+
59+
WsServer.prototype.publish = function (uri, callback) {
60+
this.store.get(uri, function (err, subscribers) {
61+
if (err) return callback(err)
62+
63+
var tasks = Object.keys(subscribers)
64+
.map(function (uuid) {
65+
return function (cb) {
66+
var client = subscribers[uri][uuid]
67+
client.send('pub ' + uri)
68+
}
69+
})
70+
71+
parallel(tasks, callback)
72+
})
73+
}

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "solid-ws",
3+
"version": "0.1.0",
4+
"description": "Web sockets for Solid",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"solid",
11+
"websockets",
12+
"live",
13+
"updates",
14+
"decentralized",
15+
"web"
16+
],
17+
"author": "Nicola Greco <me@nicola.io> (http://nicola.io/)",
18+
"license": "MIT"
19+
}

0 commit comments

Comments
 (0)