Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to push notification from server (django) to client (socketio)?
    primarykey
    data
    text
    <p>I want to emit message from server to client. I have look at this but cannot use because I cannot create a namespace instance. <a href="https://stackoverflow.com/questions/15535673/how-to-emit-socketio-event-on-the-serverside">How to emit SocketIO event on the serverside</a></p> <p>My use case is: I have a database of price of product. A lot of users are currently surf my website. Some of them is viewing product X. On the server side, the admin can edit the price of the product. If he edit the price of X, all the client must see the notification that X price change (e.x: a simple js alert).</p> <p>My client javascript now:</p> <pre><code>var socket = io.connect('/product'); #notify server that this client is viewing product X socket.emit("join", current_product.id); #upon receive msg from server socket.on('notification', function (data) { alert("Price change"); }); </code></pre> <p>My server code (socket.py):</p> <pre><code>@namespace('/products') class ProductsNamespace(BaseNamespace, ProductSubscriberMixin): def initialize(self, *args, **kwargs): _connections[id(self)] = self super(ProductsNamespace, self).initialize(*args, **kwargs) def disconnect(self, *args, **kwargs): del _connections[id(self)] super(ProductsNamespace, self).disconnect(*args, **kwargs) def on_join(self, *args): print "joining" def emit_to_subscribers(self): pass </code></pre> <p>I use the runserver_socketio.py as in this <a href="https://github.com/calvinchengx/learnsocketio" rel="nofollow noreferrer">link</a>. (Thanks to Calvin Cheng for this excellent up-to-date example.)</p> <p>I don't know how to call the emit_to_subscribers. Since I have no instance of namespace. As I read from this <a href="https://gevent-socketio.readthedocs.org/en/latest/namespace.html#module-socketio.namespace" rel="nofollow noreferrer">doc</a> , </p> <blockquote> <p>Namespaces are created only when some packets arrive that ask for the namespace. </p> </blockquote> <p>But how can I send the packet to that namespace from the code? IF I can only create the instance when a client emit message to server, when no one is surfing the site, right after finish editing the price, the system will fail.</p> <p>I am really confused about the namespace and its instance. If you have any clearer docs, please help me.</p> <p>Thanks a lot!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload