Use Redis pub/sub for channels. to @mcolyer for the idea!

This commit is contained in:
Kevin Sawicki & Nathan Sobo
2013-07-24 17:20:01 -06:00
parent 1bfb10bf2b
commit 1fc6509a51
6 changed files with 41 additions and 9 deletions

View File

@@ -68,7 +68,8 @@
"todo-tmbundle": "1.0.0",
"xml-tmbundle": "1.0.0",
"yaml-tmbundle": "1.0.0",
"nslog": "0.1.0"
"nslog": "0.1.0",
"redis": "0.8.4"
},
"devDependencies": {
"biscotto": "0.0.12",

View File

@@ -19,7 +19,7 @@ class GuestSession extends Session
start: ->
channel = @subscribe("presence-atom")
channel.on 'pusher:subscription_succeeded', =>
channel.on 'channel:open', =>
console.log 'in the channel', arguments
channel.one 'client-welcome', ({doc, siteId, repoSnapshot}) =>

View File

@@ -41,11 +41,11 @@ class HostSession extends Session
@doc = @createDocument()
channel = @subscribe("presence-atom")
channel.on 'pusher:subscription_succeeded', =>
channel.on 'channel:opened', =>
@trigger 'started'
@connectDocument(@doc, channel)
channel.on 'pusher:member_added', =>
channel.on 'channel:participant-joined', =>
@snapshotRepository (repoSnapshot) =>
welcomePackage =
siteId: @nextGuestSiteId++

View File

@@ -0,0 +1,31 @@
_ = require 'underscore'
redis = require 'redis'
guid = require 'guid'
module.exports =
class RedisChannel
_.extend @prototype, require('event-emitter')
constructor: (@name) ->
@clientId = guid.create().toString()
@subscribeClient = redis.createClient()
@sendClient = redis.createClient()
@subscribeClient.on 'error', (error) -> console.error("Error on subscribe client", error)
@sendClient.on 'error', (error) -> console.error("Error on send client", error)
@subscribeClient.subscribe @name, =>
console.log "subscribed to channel", @name
@send 'channel:participant-joined', @clientId
@trigger 'channel:opened'
@subscribeClient.on 'message', (channelName, message) =>
return unless channelName is @name
[senderId, eventName, args...] = JSON.parse(message)
unless senderId is @clientId
console.log "message on channel", eventName, args...
@trigger(eventName, args...)
send: (eventName, args...) ->
message = JSON.stringify([@clientId, eventName, args...])
@sendClient.publish(@name, message)

View File

@@ -1,14 +1,14 @@
_ = require 'underscore'
keytar = require 'keytar'
RateLimitedChannel = require './rate-limited-channel'
RedisChannel = require './redis-channel'
module.exports =
class Session
_.extend @prototype, require('event-emitter')
subscribe: (channelName) ->
new RateLimitedChannel(@getPusherConnection().subscribe(channelName))
new RedisChannel(channelName)
getPusherConnection: ->
@pusher ?= new Pusher '490be67c75616316d386',

View File

@@ -24,9 +24,9 @@ class ChannelServer
setTimeout =>
for client in @getChannelClients()
if client is channelClient
client.trigger 'pusher:subscription_succeeded'
client.trigger 'channel:opened'
else
client.trigger 'pusher:member_added'
client.trigger 'channel:participant-joined'
channelClient
getChannelClients: -> _.values(@channelClients)
@@ -53,7 +53,7 @@ class ChannelClient
send: (eventName, eventData) ->
@channelServer.send(this, eventName, eventData)
fdescribe "Collaboration", ->
describe "Collaboration", ->
describe "joining a host session", ->
[hostSession, guestSession, pusher, repositoryMirrored] = []