Linux Tips

Setting the $JWEBSOCKET_HOME variable in linux

The jWebSocketServer-<version>.jar includes all required libraries and provides the ready-to-use folder structure as described here. The next step is to tell the system to make a reference to this folder as an Environment Variable that will be used later by java to execute jWebSocketServer. The steps to export a home variable in linux are very simple:

1- If you are in this section you should have probably downloaded the jWebSocketServer-<version>.zip from the downloads section, so, the next step is extract it into the folder /opt/jWebSocket-<version> you will see that the created directory has the same structure as defined here.
2- If necessary set writing permissions to the created folder:

sudo chmod -R 755 /opt/jWebSocket-<version>

3- Export the JWEBSOCKET_HOME variable for all the system:

You need to access to a file that loads at linux startup, for example /etc/profile or /etc/bash.bashrc file for all users, if you want the JWEBSOCKET_HOME variable only for your user then you should edit the file $HOME/.bashrc. In new Ubuntu versions like Ubuntu 12.10 you will find a file created explicitly for writing your environment variables, you can edit the file /etc/environment, so, make your choice from the options you read before and edit the file you want:

sudo gedit $HOME/.bashrc

Note: if you don't have gedit you can use then "nano", "leafpad" or any editor you know.

4- Add the JWEBSOCKET_HOME variable at the end of the file:

export JWEBSOCKET_HOME=/opt/jWebSocket-<version>/

Save your changes and restart the system, so it can recognize the new variable.

If you want to see that was successful, after restarting open a terminal and write:

You should get a result in the console like: /opt/jWebSocket-<version>/ then you did the tutorial correctly, so you can continue to run jWebSocket server as .jar.

If this tutorial is not clear to you, you can find more detailed information of how to set an environment variable in linux here.

About jWebSocket and FlashBridgePlugIn in linux

If you are a deveoloper and you want to deploy your jWebSocket server in linux you should know that jWebSocket server offers a Flash Player based WebSocket communication for browsers that doesn't support this protocol. Flash Player 9 (version and supperior) implement an access policy for Flash applications that interact with the server. Flash Player uses this policy to let server owners decide which SWF files are allowed to interact with the server and control what those SWF files can do when interacting with the server.

When the Flash Player tries to make a connection, it checks the port 843, but nonroot users aren’t allowed to listen on ports below 1024 on Linux. If you are the administrator of your jWebSocket server and of course, if you have root access, then you can run jWebSocketServer with super user privileges to listen on this port and return a server-wide socket policy.
So, most of the times in linux when you run your jWebSocketServer you will find that an error is shown in the server logs:

ERROR - FlashBridgePlugIn: FlashBridge could not be started: Permission denied
How to remove this error:

1- Open the jWebSocket.xml configuration file, go to the flashbridge plugin cofiguration section and set the port to 8843 or any other that you like in the port settings configuration tag:

<!-- FlashBridge-Plug-in to support WebSocket communication on older browsers via flash -->
	  <!-- if you remove this setting the plug-in uses an internal "allow all ports and domains" default crossdomain.xml -->
	  <setting key="crossdomain_xml">${JWEBSOCKET_HOME}conf/FlashPlugIn/crossdomain.xml</setting>
          <!-- Please don't change this configuration if not necessary -->
          <!-- In case that you are running the server from Linux you will need special privileges to use any port < 1024, for special requirements in Linux OS -->
	<setting key="port">8843</setting>

Forwarding port 843 to 8843:

When a new connection comes to the server from the browser flash player, uses the port 843 to send the request, but as it's access is locked for nonroot users, we should redirect incoming packets to the defined port 8843, so, to do it is very simple, open a terminal window and type the following:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 843 -j REDIRECT --to-port 8843

After redirecting all incoming data from the above port is also important to respond to the FlashPlayer WebSocket interface using the same compatible port 843, that's why you should redirect the output connections also from the port 8843 to the port 843 in the following way:

sudo iptables -t nat -A OUTPUT -p tcp -m tcp --dport 843 -j REDIRECT --to-ports 8843

After you already have the iptables proxy configuration and port forwarding correctly established you should probably want to make this configurations permanent, then you should read the section saving iptables from ubuntu forums.


Learn more about WebSockets in general, get background information and gain deeper insight!

Join jWebSocket

Wether developer, designer or translator – join the jWebSocket team and grow together with our success!

Copyright © 2013 Innotrade GmbH. All rights reserved.