Monday, March 16, 2015

Websocket

Websocket can be used to create real time chat and game services.

Autobahn|Python [3] is a WebSocket / WAMP library for Python 2 (using Twisted) and 3 (using asyncio). It's easy to setup and use.

Websocket and browser support [1]:

WebSocket is a protocol providing full-duplex communications channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API in Web IDL is being standardized by the W3C.

WebSocket is designed to be implemented in web browsers and web servers, but it can be used by any client or server application. The WebSocket Protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request.[1] The WebSocket protocol makes more interaction between a browser and a website possible, facilitating live content and the creation of real-time games. This is made possible by providing a standardized way for the server to send content to the browser without being solicited by the client, and allowing for messages to be passed back and forth while keeping the connection open. In this way a two-way (bi-directional) ongoing conversation can take place between a browser and the server. A similar effect has been achieved in non-standardized ways using stop-gap technologies such as Comet.

In addition, the communications are done over TCP port number 80, which is of benefit for those environments which block non-web Internet connections using a firewall. The WebSocket protocol is currently supported in most major browsers including Google Chrome, Internet Explorer, Firefox, Safari and Opera. WebSocket also requires web applications on the server to support it.



Turn on websockets in firefox [2]:

1. Type about:config in address bar, and continue by clicking “I’ll be careful, I promise”
2. Set network.websocket.enabled  value to ‘true’ and set network.websocket.override-security-block preferences to ‘true’.
3. Restart Firefox browser.

== Change a websocker server to daemon ==

Each time you can run this to start a websocket server: python ./server.py

To change it to a daemon you can do this:
- add "#!/usr/bin/python" to top of server.py
- install daemonize [4]
- use absolute path of server.py, run this: daemonize /../server.py

Now server.py is started as a daemon. To find out which process it's running as and to shut it down, do:

- ps -aux | grep server.py

This will (usually, if you don't have other process with the name "server") find 2 entries, one for the server.py process, one for the grep command. Pick the former, you can watch its activity by:

- top -p [pid]

To kill it:

- kill -9 [pid]


[1] http://en.wikipedia.org/wiki/WebSocket
[2] http://techdows.com/2010/12/turn-on-websockets-in-firefox-4.html
[3] http://autobahn.ws/
[4] http://software.clapper.org/daemonize/ 
[5] websocket browser support

No comments:

Blog Archive

Followers