Comments (9)
Hi Anders,
Good question regarding Ruby and sync publish. We are talking here at PubNub and may respond with more questions for you shortly.
Cheers!
Stephen Blum
http://twitter.com/PubNub
https://github.com/pubnub/pubnub-api - GitHub
www.pubnub.com
On Apr 5, 2013, at 7:50 AM, Anders Bengtsson [email protected] wrote:
Hi,
I need a way to make a synchronous publish() call, so that I can use the PubNub response in my own HTTP response.
I've tried this:
q = Queue.new pn.publish(channel: channel, message: message, callback: lambda { |response| q << response }, origin: origin_hostname) p q.pop
but pop() immediately raises a deadlock error, since there's only one thread(!?). Any ideas what I can do to block until the publish has finished?
This is with pubnub 3.3.0.7.—
Reply to this email directly or view it on GitHub.
from ruby.
@ghostganz which app server are you using?
from ruby.
This is on Rails with Thin, with Ruby 1.9.3.
from ruby.
Try to use Thread.join
with Thread.list
to identify which threat you want to join, if you want to join all threads it looks like:
def join_all main = Thread.main # The main thread current = Thread.current # The current thread all = Thread.list # All threads still running # Now call join on each thread all.each{|t| t.join unless t == current or t == main } end
from ruby.
When I run Thread.main, Thread.current and Thread.list directly after calling publish() they all give me the same one thread, so that doesn't help me. I guess it uses some other mechanism than threads to do things asynchronously.
Ideally publish() would have a flag to make it blocking instead of asynchronous, but in place of that I need some way to do the blocking myself.
from ruby.
Thanks @blazeroot! Since most often than not simple is the most elegant solution... let me see if I can get you a blocking synchronous publish version ASAP. Standby...
from ruby.
@ghostganz I just pushed a candidate 3.3.0.8 which has an optional :sync_http initializer option:
pn = Pubnub.new(:publish_key => :demo, :subscribe_key => :demo, :sync_http => true)
This will push all publish requests through a blocking httparty request, instead of the default em-http.
Please let me know if this helps you.
geremy
from ruby.
Thanks a lot @geremyCohen, that seems to work!
Minor detail: You could make "callback" a non-mandatory attribute to publish() when sync_http is true. I have to use a no-op lambda there now.
from ruby.
@ghostganz awesome, and yes, great suggestion re: the no-op. Our 3.4 version is actually under development right now, so I will be sure to make the callback optional in this scenario.
I'll close this one out, please ping us here or via support at pubnub if you have any additional questions or issues.
from ruby.
Related Issues (20)
- Publishing message error HOT 7
- Unsubscribe? HOT 4
- Exception inside SubscribeCallback is getting ignored
- presence method does not get a callback on join events HOT 7
- "less than 100ms" HOT 4
- PubNub 3.4 for Ruby Released HOT 3
- Hardcoded log file HOT 4
- uninitialized constant Pubnub::Configuration::PUBNUB_VERSION HOT 8
- This application keeps requesting messages from the database forever HOT 5
- Multi-process access in Heroku environment causes PubNub initialization to occasionally fail HOT 7
- Problems with PAM ttl in 3.5.0beta HOT 2
- 3.5.0 Release Candidate Available
- Logger invocations should use progname HOT 4
- event machine not initialized exception HOT 2
- Message object does allow values to be arrays HOT 3
- MXing with Ruby
- Publish on multiple channels doesn't work HOT 4
- Error on publish "eventmachine not initialized" HOT 19
- Retrieve message uuid from ruby api HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ruby.