0x7466 / python-actioncable-zwei Goto Github PK
View Code? Open in Web Editor NEWAction Cable Client for Python 3
License: MIT License
Action Cable Client for Python 3
License: MIT License
Hi! Iḿ having a similar problem, i get connected to my localhost server but my subscription is stuck at pennding state, so i do not recieve any messages nor can send them.
from actioncable.connection import Connection
from actioncable.subscription import Subscription
connection = Connection(url='ws://localhost:3000/cable', origin='http://localhost:3000')
connection.connect()
sleep(3)
print(connection.connected) #this is True
subscription = Subscription(connection, identifier={'channel': 'RaspiCommChannel'})
subscription.create()
If I check subscription state is 'pending' allways.
I try it from python console, using Python 3.7.2.
Can you help me please? Thanks!
Originally posted by @eterrones in #4 (comment)
Using:
I am using ActionCable on webpage which works fine in rails, now trying to connect from python using https://github.com/tobiasfeistmantl/python-actioncable-zwei ActionCableZwei - A Python 3 client for Rails' Action Cable
Below line connects successfully:
connection = Connection(url='ws://localhost:4000/cable', origin='http://localhost:4000/')
and I get proper logs in rails.
Now, I want to send cookies with sockets but Below line is unable to connect nor do I get any logs in rails.
connection = Connection(url='ws://localhost:4000/cable', origin='http://localhost:4000/', log_ping=False, cookie=coo)
Also tried using log_ping to True/False
Below is full code that am trying:
from actioncable.connection import Connection
from actioncable.subscription import Subscription
from actioncable.message import Message
import datetime
import random
from time import sleep
import Cookie
coo = Cookie.SimpleCookie()
coo["fig"] = "newton"
coo["sugar"] = "wafer"
# connection = Connection(url='ws://localhost:4000/cable', origin='http://localhost:4000/')
connection = Connection(url='ws://localhost:4000/cable', origin='http://localhost:4000/', cookie=coo)
connection.connect()
subscription = Subscription(connection, identifier={'channel': 'TestChannel'})
subscription.create()
sleep(30)
subscription.remove()
connection.disconnect()
I just don't get any ping/log in rails about trying to connect. How can I fix this?
Hello.
I tried to test the example, but it didn't work and I found out why.
There were mismatching parameters because of the self
in the callback. So I made a small change: I changed the callbacks for lambdas to avoid the conflict with self
diff --git a/actioncable/connection.py b/actioncable/connection.py
index 1190696..08c6507 100644
--- a/actioncable/connection.py
+++ b/actioncable/connection.py
@@ -85,9 +85,9 @@ class Connection:
self.url,
cookie=self.cookie,
header=self.header,
- on_message=self._on_message,
- on_close=self._on_close)
- self.websocket.on_open = self._on_open
+ on_message=lambda socket, message : self._on_message(socket, message),
+ on_close=lambda socket : self._on_close(socket))
+ self.websocket.on_open = lambda socket : self._on_open(socket)
self.websocket.run_forever(ping_interval=5, ping_timeout=3, origin=self.origin)
time.sleep(2)
I hope this helps
I'm occasionally seeing messages "Connection not established. Add message to queue.", which is based off the channel still being in a 'pending' state, meaning the code never received a 'subscribed' message from the server.
BUT I am seeing the ActionCable server did send that subscribed message
! I'm not sure why py-actioncable isn't receiving this subscribed message, and therefore pulling the channel out of 'pending'.
I see this issue locally and when talking to a dockerized server. I'd love to provide sample code, but it's a little hard to reproduce. This dance works flawlessly the first time, and generally happens when there is an unhealthy connection ping, and py-actioncable attempts to reconnect.
Do you get this issue as well sometimes?
When I use the Subscription example verbatim, the callback
variable is missing, giving an error.
Expected: Connection to URL specified, and an attempt to subscribe to named channel.
Example code:
from actioncable.connection import Connection
from actioncable.subscription import Subscription
connection = Connection(url='ws://localhost:3000/cable', origin='http://localhost:3000')
connection.connect()
subscription = Subscription(connection, channel_name='AppChannel', identifier={'additional': 'params'}) # You don't have to provide the channel name in the identifier param.
def on_receive(message):
print('New message arrived!')
print('Action: {} | Data: {}'.format(message.action, message.data))
subscription.on_receive(callback)
subscription.create()
Following the README verbatim, and pasting in the examples, I'm getting an error finding the modules. I'm new to Python, but something seems off.
Traceback (most recent call last): File "./actioncable.py", line 1, in <module> from actioncable.subscription import Subscription File "/Users/nic/programming/test_python/actioncable.py", line 1, in <module> from actioncable.subscription import Subscription ModuleNotFoundError: No module named 'actioncable.subscription'; 'actioncable' is not a package
Also: Thanks for writing this! I lost the entire morning thinking that ActionCable was so great, only to realize that as always, it only works well inside a Rails world where all the parameters are abstracted away. :(
I'm still getting the hang of github, so I apologize if posting this as an issue is breaking etiquette or if I am supposed to mention it another way?
This package is very useful, but unfortunately the readme hurts more than it helps.
When I copy the examples and run them, nothing happens. Meaning, my rails server shows no connection. I know the URL to be correct
Using the example Connect code
from actioncable.connection import Connection
from actioncable.subscription import Subscription
connection = Connection(url='ws://localhost:3000/cable', origin='http://localhost:3000')
connection.connect()
When I run this, the rails server shows no new connections
Puma starting in single mode...
* Version 3.8.2 (ruby 2.3.0-p0), codename: Sassy Salamander
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.