chat application

178 views
Skip to first unread message

Clavier

unread,
Sep 28, 2013, 9:22:22 AM9/28/13
to pyjs-...@googlegroups.com
Hello. 

What is the best way to build a chat application in Pyjs framework? I cannot find a demo or example for chat or relevant application in Pyjs.  I need callback features (server-to-client call). It seems that JsonRPC and Web.py don't support callback.

I built it using Pyro4 library supporting callback. It worked beautifully in desktop (Pyjd), but I found that Pyro4 didn't work in Pyjs for webbrowser ("ImportError: No module named selelect" (which is a socket library)).  If Pyjs doesn't support socket communication in client side (or callback to client), what is the best way to build a chat application requiring callback features?

Thanks in advance


Apexi 200sx

unread,
Sep 28, 2013, 11:32:12 AM9/28/13
to pyjs-...@googlegroups.com
When you run your app using pyjd it is python code that is executing and because you are not running within the constraints of a browser you have access to any python libraries you wish to use, e.g. Pyro4 is using the select library for monitoring activity on network sockets which I assume it uses for communication. 

When you compile your code using pyjs you are generating javascript code from your python source code which you then execute in a web browser.  Note you are running javascript in a web browser so you have all the usual limitations of web browsers (e.g. no access to low level socket interface) you also only have access to any libraries that are pure python (no libraries written as C extensions will work)  and don't do things like write to local file system or try and use sockets.




--
 
---
You received this message because you are subscribed to the Google Groups "Pyjs.org Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyjs-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Clavier

unread,
Sep 28, 2013, 10:24:53 PM9/28/13
to pyjs-...@googlegroups.com
I understand the limitation in a web browser.  Anyway there are a lot of chat applications running in web browsers.  How can a chat application be built in Pyjs if incomming socket communication is not allowed?

I took a look at chat applications in web browsers supporting socket communication. Socket.IO or WebSocket were used and they were Javascript. Tornado is python library for asynchronous call supporting callback, but it uses javascript for Socket.IO.


I think that socket communication had been limited in a web browser in the past, but not anymore in recent days. I believe that Pyjs should support socket library for a web browser. Otherwise, chat applications and other interactive web applications cannot be built. 

I will try to integrate Socket.IO and Pyjs (or Tornado and Pyjs). Anyway It is not pure python because they use javascript for socket communication. I like the Pyjs philosophy using everything in python.  I appreciate any comment about building a chat application in Pyjs framework. 

Thanks again

Glyph

unread,
Sep 29, 2013, 1:17:26 AM9/29/13
to pyjs-...@googlegroups.com
On Sep 28, 2013, at 7:24 PM, Clavier <bachc...@gmail.com> wrote:

I understand the limitation in a web browser.  Anyway there are a lot of chat applications running in web browsers.  How can a chat application be built in Pyjs if incomming socket communication is not allowed?

There are lots of options.

The oldest (and still most reliable cross-browser) technique is something called "long polling" <https://en.wikipedia.org/wiki/Push_technology#Long_polling>; basically, you can issue a request with PyJS's built in HTTPRequest class, and have the server leave that request un-answered until it has something to say to the client.  If the client just does this forever in a loop, then the server can answer at its leisure.

This can be written with no new JavaScript code, not even any small JS(...) wrappers, since PyJS ships with an existing HTTP request wrapper.

I took a look at chat applications in web browsers supporting socket communication. Socket.IO or WebSocket were used and they were Javascript. Tornado is python library for asynchronous call supporting callback, but it uses javascript for Socket.IO.

My suggestion is to build a client and a server that can communicate using multiple techniques.  I would start with long polling rather than Socket.IO or Tornado for several reasons:

  1. You can do this in PyJS today, just fine, with no new JavaScript code.
  2. If you want this to work reliably in multiple browsers, you will have to build a fallback just fine.
  3. Because protocols for websockets and browser APIs for exposing two-way communication are evolving, you really need to build an abstraction within your PyJS framework for encapsulating the message-channel part of your protocol.  You don't want to expose either WebSocket _or_ Long Polling as your API, but rather, give applications a generic API of your own design and provide multiple implementations so that you can choose the best one available in a given browser.  Long polling is the lowest common denominator that works everywhere, so it's the best one to start with.


I think that socket communication had been limited in a web browser in the past, but not anymore in recent days.

Socket communication is still quite limited.  "WebSockets" are not "sockets in a web browser", but rather a new protocol, <http://www.rfc-editor.org/rfc/rfc6455.txt>.  If you want to talk to a server speaking an existing protocol, you still need a server-side proxy that will change WebSocket data into actual socket communication for you.

I believe that Pyjs should support socket library for a web browser. Otherwise, chat applications and other interactive web applications cannot be built.

I agree.  It would be great if someone would make such a thing!

I will try to integrate Socket.IO and Pyjs (or Tornado and Pyjs). Anyway It is not pure python because they use javascript for socket communication. I like the Pyjs philosophy using everything in python.  I appreciate any comment about building a chat application in Pyjs framework. 

Although for your purposes it will be very similar to Tornado, I'd love it if you would also consider integrating Twisted <https://twistedmatrix.com/>.

Thanks again

Good luck with your work, and please keep sending messages to this list!  (And contributing pull requests to PyJS itself...)

-glyph

Clavier

unread,
Oct 5, 2013, 11:54:28 PM10/5/13
to pyjs-...@googlegroups.com
I was able to build a web RPC module similar to Pyro4 based on the long-polling technique you mentioned. It was built by only using Pyjs's HTTPRequest.asyncPost(). No javascript code was needed. It is a simplified web version of Pyro4.  So, I think that I can transplant the existing chat module running on Pyro4 into Pyjs easilly. I will go further and post the result here soon.

Thanks for directing me into the right direction.  I was impressed that a web chat application could be made by only using Pyjs HTTPRequest.

Glyph

unread,
Oct 7, 2013, 1:35:14 PM10/7/13
to pyjs-...@googlegroups.com

On Oct 5, 2013, at 8:54 PM, Clavier <bachc...@gmail.com> wrote:

> I was able to build a web RPC module similar to Pyro4 based on the long-polling technique you mentioned. It was built by only using Pyjs's HTTPRequest.asyncPost(). No javascript code was needed. It is a simplified web version of Pyro4. So, I think that I can transplant the existing chat module running on Pyro4 into Pyjs easilly. I will go further and post the result here soon.

For what it's worth, Pyro has very dangerous security properties and you should not be exposing an API like that to a web browser.

You may be interested in reading this:

<http://glyph.twistedmatrix.com/2008/07/static-on-wire.html>

> Thanks for directing me into the right direction. I was impressed that a web chat application could be made by only using Pyjs HTTPRequest.

Cool!

It's still probably worth implementing more modern stuff like websocket to optimize this, beneath whatever API you've put on top of asyncPOST. But this is a good place to start and I'm glad it worked out well for you.

-glyph

Clavier

unread,
Nov 7, 2013, 12:39:16 AM11/7/13
to pyjs-...@googlegroups.com
I have done implementing a chat application.  It took longer than I expected because I had to do it applying asynchronous programming in client side.  It seems that Pyjs doesn't support multithreading, so asynchronous programming was the only way I can write code simpler. I will post a new issue about it later.

To summarize,  a chat application was built using Pyjs's HTTPRequest.asyncPost() based on the long polling technique. Furthermore, Pyro-style multiprocessing library was built too. I considered implementing websocket protocol too, but Pyro-style was a lot simpler making code straightforward. Pyro protocol includes passing callbacks and return objects between a server and clients, and it enables to write server-client programming without changing much from standalone programming.

Thanks.

Clavier

unread,
Nov 23, 2013, 11:20:45 PM11/23/13
to pyjs-...@googlegroups.com
I found that my chat application died periodically due to the default time-out setting maintaining http connection. I cannot find a way to change time-out in HTTPRequest.  It is essential to the long-polling technique to maintain http connection longer. Please let me know if you know about it.  (I posted it in the bug report page: https://github.com/pyjs/pyjs/issues/808)

Thanks again.
Reply all
Reply to author
Forward
0 new messages