Skip to content

Commit

Permalink
Remove async connections
Browse files Browse the repository at this point in the history
This library is not thread-safe
  • Loading branch information
zimbatm committed May 14, 2015
1 parent f269fcb commit 4cbc3ac
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 65 deletions.
29 changes: 0 additions & 29 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,6 @@ The application will pause at socket.connect and handle events from Pusher as th

socket.connect

== Asynchronous Usage
The socket will remain open in the background as long as your main application thread is running,
and you can continue to subscribe/unsubscribe to channels and bind new events.

require 'pusher-client'
socket = PusherClient::Socket.new(YOUR_APPLICATION_KEY)
socket.connect(true) # Connect asynchronously

# Subscribe to two channels
socket.subscribe('channel1')
socket.subscribe('channel2')

# Bind to a global event (can occur on either channel1 or channel2)
socket.bind('globalevent') do |data|
puts data
end

# Bind to a channel event (can only occur on channel1)
socket['channel1'].bind('channelevent') do |data|
puts data
end

loop do
sleep(1) # Keep your main thread running
end

For further documentation, read the source & test suite. Some features of the JavaScript client
are not yet implemented.

== Native extension
Pusher depends on the websocket[https://github.com/imanel/websocket-ruby] gem
which is a pure Ruby implementation of websockets.
Expand Down
22 changes: 0 additions & 22 deletions examples/hello_pusher_async.rb

This file was deleted.

19 changes: 5 additions & 14 deletions lib/pusher-client/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,19 @@ def initialize(app_key, options={})
end

def connect(async = false)
raise "Async is not supported anymore" if async
return if @connection
logger.debug("Pusher : connecting : #{@url}")

if async
@connection_thread = Thread.new do
begin
connect_internal
rescue => ex
send_local_event "pusher:error", ex
end
@connection_thread = nil
end
else
connect_internal
end
connect_internal
self
end

def disconnect(ex = nil)
return unless @connection
logger.debug("Pusher : disconnecting")

@connection.close
@connection.close rescue nil

send_local_event("pusher:connection_disconnected", ex)
end
Expand Down Expand Up @@ -215,8 +205,9 @@ def connect_internal
send_local_event(params['event'], params['data'], params['channel'])
end
end
rescue IOError, Errno::EBADF => ex
rescue IOError, Errno::EBADF, SocketError => ex
disconnect(ex)
return ex
end

def send_local_event(event_name, event_data, channel_name=nil)
Expand Down

0 comments on commit 4cbc3ac

Please sign in to comment.