mirror of
https://github.com/atom/atom.git
synced 2026-01-23 13:58:08 -05:00
Use Redis pub/sub for channels. ⚡ to @mcolyer for the idea!
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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}) =>
|
||||
|
||||
@@ -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++
|
||||
|
||||
31
src/packages/collaboration/lib/redis-channel.coffee
Normal file
31
src/packages/collaboration/lib/redis-channel.coffee
Normal 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)
|
||||
@@ -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',
|
||||
|
||||
@@ -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] = []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user