websockets

1,785 views
Skip to first unread message

shartha

unread,
Jun 13, 2013, 9:36:31 PM6/13/13
to web...@googlegroups.com
Hello. Some questions about websockets:
1) Can websockets be implemented within web2py without any in-socket server side component?
2) If yes, how is the constructor used on a local machine in the constructor, say, as: 
var ws = new WebSocket("ws://localhost/echo");

Thank you!

Massimo Di Pierro

unread,
Jun 13, 2013, 11:50:18 PM6/13/13
to web...@googlegroups.com


On Thursday, 13 June 2013 20:36:31 UTC-5, shartha wrote:
Hello. Some questions about websockets:
1) Can websockets be implemented within web2py without any in-socket server side component?

The problem is that it needs an async server to be any useful. Rocket is not async, it is multi-threaded. 

samuel bonilla

unread,
Jun 14, 2013, 12:30:20 AM6/14/13
to web...@googlegroups.com
Web2py + node.js

António Ramos

unread,
Jun 14, 2013, 4:28:18 AM6/14/13
to web...@googlegroups.com


2013/6/14 samuel bonilla <pytho...@gmail.com>
--
 
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Roberto De Ioris

unread,
Jun 14, 2013, 9:40:06 AM6/14/13
to web...@googlegroups.com
> -

uWSGI exposes a very fast websockets api you can easily use in web2py too.

The problem is that once you complete the websocket handshake a whole
thread will remains busy until the connection is not closed.

You can reduce the problem spawning multiple threads, but you need a good
amount of memory.

This is an example echo server:

https://github.com/unbit/uwsgi/blob/master/tests/websockets_echo.py

basically you call uwsgi.websocket_handshake to ack the connection and
then you can use uwsgi.websocket_recv and uwsgi.websocket_send


--
Roberto De Ioris
http://unbit.it

Ricardo Pedroso

unread,
Jun 14, 2013, 4:50:08 PM6/14/13
to web...@googlegroups.com
On Fri, Jun 14, 2013 at 2:36 AM, shartha <m.mirg...@gmail.com> wrote:
Hello. Some questions about websockets:
1) Can websockets be implemented within web2py without any in-socket server side component?
 
Yes. Runnig web2py with gevent or eventlet 

If you want I can post an example using socketio.

Ricardo

shartha

unread,
Jun 15, 2013, 8:07:19 PM6/15/13
to web...@googlegroups.com
Hi Ricardo. I would love to have a functional example. Could you please make one?

Thanks!

Ricardo Pedroso

unread,
Jun 19, 2013, 1:24:52 PM6/19/13
to web...@googlegroups.com
On Sun, Jun 16, 2013 at 1:07 AM, shartha <m.mirg...@gmail.com> wrote:
Hi Ricardo. I would love to have a functional example. Could you please make one?


Sorry for the delay.... here it goes a functional example.
 

1. Install requirements
--------------------------------

$ pip install gevent
$ pip install gevent-socketio

2. go to your web2py folder and....
--------------------------------------------------

in routes.py you need to add the socket.io endpoint, eg:

routes_in = (
    ('/socket.io/$anything', '/iochat/default/socketio/$anything'),
    ....
   )


3. copy&paste lines below into run_iochat.py
-----------------------------------------------------------------

from gevent import monkey; monkey.patch_all()

from socketio import socketio_manage
from socketio.server import SocketIOServer
from socketio.namespace import BaseNamespace
from socketio.mixins import RoomsMixin, BroadcastMixin

from gluon.main import wsgibase

if __name__ == '__main__':
    print 'Listening on port 8080'
    SocketIOServer(('0.0.0.0', 8080), wsgibase,
        resource="socket.io").serve_forever()


4. unzip attachement into web2py/applications
--------------------------------------------------------------------

the application is called iochat so make sure you don't have one.


5. return to web2py folder
-------------------------------------

execute:

$ python run_iochat.py


------------------------------------------------------------------------------------

- register some users
- create at least one room

Now play with the chat from two different browsers. (I only try it with firefox and chromium)


I think that's all, any question feel free to ask.


Regards,
Ricardo




iochat.zip

shartha

unread,
Jun 21, 2013, 1:14:57 PM6/21/13
to web...@googlegroups.com
Hi Ricardo,

Thank you very much for posting the application. Here are a few questions that I have regarding this:
1. Do I need to put the route.py in the root directory of my app?
2. I tried by putting the routes.py in the rood directory of iochat. run_io_chat.py was also created in the same directory. I also included 

import sys
sys.path.append('path_to_web2py\library.zip')

at the very top of your run_io_chat.py. When I run the command python.exe run_io_chat.py, I get the error: Cannot determine web2py version.
Also when I try to visit http://127.0.0.1:8000/iochat/default/index, I get the following ticket:

<type 'exceptions.ImportError'> No module named socketio.namespace


Could you tell me what I am doing wrong?

Thanks!

Ricardo Pedroso

unread,
Jun 23, 2013, 1:06:18 PM6/23/13
to web...@googlegroups.com
On Fri, Jun 21, 2013 at 6:14 PM, shartha <m.mirg...@gmail.com> wrote:
Hi Ricardo,

Thank you very much for posting the application. Here are a few questions that I have regarding this:
1. Do I need to put the route.py in the root directory of my app?
2. I tried by putting the routes.py in the rood directory of iochat. run_io_chat.py was also created in the same directory. I also included 

routes.py and run_io_chat.py in the web2py folder not in the app folder.

 
import sys
sys.path.append('path_to_web2py\library.zip')

at the very top of your run_io_chat.py. When I run the command python.exe run_io_chat.py, I get the error: Cannot determine web2py version.
Also when I try to visit http://127.0.0.1:8000/iochat/default/index, I get the following ticket:

<type 'exceptions.ImportError'> No module named socketio.namespace


You should use web2py sources (not the binary one) and install gevent and gevent-sockeio.
Also the port defined in run_io_chat.py, if you do not change it, is 8080.


Ricardo

shartha

unread,
Jul 3, 2013, 3:15:13 PM7/3/13
to web...@googlegroups.com
Thank you Recardo. I am sorry for being late to reply back. I appreciate your time and help. 

bha...@kivacybersecurities.com

unread,
Oct 24, 2013, 4:59:34 AM10/24/13
to web...@googlegroups.com
Hii Ricardo,

Ruud Schroen

unread,
Apr 28, 2014, 10:30:34 AM4/28/14
to web...@googlegroups.com
Web2Py seems to start, but everytime i load the page i get a ticket.

When i try to open the ticket it says:

admin disabled because unable to access password file

My routes.py is like this:

routes_in = (
    ('/socket.io/$anything', '/iochat/default/socketio/$anything'),
)


黄祥

unread,
Jan 17, 2016, 4:52:37 PM1/17/16
to web2py-users
i've followed the steps but end it with an error occured :
running run_iochat.py from terminal

(site)MacBook:~ MacBookPro$ python ~/site/web2py/run_iochat.py

Traceback (most recent call last):

  File "/Users/MacBookPro/site/web2py/run_iochat.py", line 8, in <module>

    from gluon.main import wsgibase

  File "/Users/MacBookPro/site/web2py/gluon/main.py", line 121, in <module>

    raise RuntimeError("Cannot determine web2py version")

RuntimeError: Cannot determine web2py version


when tried to access from web2py site

Traceback (most recent call last):
File "/Users/MacBookPro/site/web2py/gluon/restricted.py", line 227, in restricted
exec ccode in environment
File "/Users/MacBookPro/site/web2py/applications/iochat/models/db.py", line 14, in <module>
import chat
File "/Users/MacBookPro/site/web2py/gluon/custom_import.py", line 85, in custom_importer
modules_prefix, globals, locals, [itemname], level)
File "applications/iochat/modules/chat.py", line 8, in <module>
from gluon.dal import DAL, BaseAdapter, Field
ImportError: cannot import name BaseAdapter


any idea or solution for this?


thanks and best regards,

stifan

Massimo Di Pierro

unread,
Jan 17, 2016, 8:14:41 PM1/17/16
to web2py-users
I think it is in 

from pydal.adapters.base import BaseAdapter
Reply all
Reply to author
Forward
0 new messages