Giter VIP home page Giter VIP logo

passenger-nodejs-websocket-demo's Introduction

Phusion Passenger: Node.js WebSocket demo

This application demonstrates WebSocket support in the Phusion Passenger application server for Node.js, through the use of the awesome Socket.io library. Passenger supports all major modern web technologies, such as WebSockets, entirely out of the box. You don't have to do anything: WebSocket support just works.

If you like this demo, please tweet about it or follow us on Twitter.

See also: >> What is Phusion Passenger, and why should I care?

Node.js

Phusion Passenger

Getting started

Preparations

  1. Install Phusion Passenger 4.0.45 or later.

  2. Clone this repository:

    git clone https://github.com/phusion/passenger-nodejs-websocket-demo.git
    cd passenger-nodejs-websocket-demo
    
  3. Install dependencies:

    npm install
    

Running the demo in Passenger Standalone

Run:

passenger start --sticky-sessions

Access the demo application at http://0.0.0.0:3000/ and see it in action.

Sticky sessions are required for long polling support.

Running the demo in Passenger for Nginx

Create a virtual host in your Nginx configuration file:

server {
    listen 3000;
    server_name passenger-nodejs-websocket.demo;
    root /path-to/passenger-nodejs-websocket-demo/public;
    passenger_enabled on;

    # Sticky sessions are required for long polling support!
    passenger_sticky_sessions on;
}

Add passenger-nodejs-websocket.demo to your /etc/hosts:

echo 127.0.0.1 passenger-nodejs-websocket.demo | sudo tee -a /etc/hosts

Then restart Nginx, and access the demo application at http://passenger-nodejs-websocket.demo:3000/

Running the demo in Passenger for Apache

Phusion Passenger currently does not support WebSockets on Apache. Having said that, Socket.io gracefully falls back to long polling when run on Apache, so the demo still works.

Create a virtual host in your Apache configuration file:

# Comment out if you already have a "Listen 3000" directive somewhere.
Listen 3000
# Comment out when using Apache >= 2.4.
NameVirtualHost *:3000

<VirtualHost *:3000>
    ServerName passenger-nodejs-websocket.demo
    DocumentRoot /path-to/passenger-nodejs-websocket-demo/public

    # Sticky sessions are required for long polling support!
    PassengerStickySessions on
</VirtualHost>

Add passenger-nodejs-websocket.demo to your /etc/hosts:

echo 127.0.0.1 passenger-nodejs-websocket.demo | sudo tee -a /etc/hosts

Then restart Apache, and access the demo application at http://passenger-nodejs-websocket.demo:3000/

Running the demo in Node.js without Passenger

Run:

node app.js

Then access the demo application at http://0.0.0.0:3000/

Next steps

Please enjoy Phusion Passenger, a product by Phusion. :-)

About Phusion Passenger


5 minute intro: Making Node.js deployment enjoyable (DotJS Paris 2013)


Phusion Passenger used in Game of Thrones Ascention

Phusion Passenger™ is a web server and application server, designed to be fast, robust and lightweight. It takes a lot of complexity out of deploying web apps, adds powerful enterprise-grade features that are useful in production, and makes administration much easier and less complex. Phusion Passenger supports Ruby, Python, Node.js and Meteor, and is being used by high-profile companies such as Apple, Pixar, New York Times, AirBnB, Juniper etc as well as over 350.000 websites.

Learn more: Website | Documentation | Support resources | Github | Twitter | Blog

passenger-nodejs-websocket-demo's People

Contributors

duritong avatar foobarwidget avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

passenger-nodejs-websocket-demo's Issues

socket.io.js not being served.

I have the following nginx configuration:

server {
  listen 80;
  root /home/kpowers/app/public;
  passenger_enabled on;
  passenger_app_type node;
  passenger_startup_file app.js;
}

The app starts and the root path is loaded but the client receives a 404 for socket.io.js:

image

The following entry appears in the nginx error log:

[error] 1042#0: *5 open() "/home/kpowers/app/public/socket.io/socket.io.js" failed (2: No such file or directory)

The app works fine on its own when running without Passenger. My guess is that Passenger sees that the request is for a file and doesn't expect that the app is supposed to serve it. I checked the Passenger Nginx Reference Docs but didn't see anything (at first glance) about sending certain file requests through to the app.

Thoughts?

websocket corrupted by chunked encoding

Trying this nodejs demo on apache and mod_passenger. But socket.io is only falling back to polling as the websocket gets corrupted by apache sending the upgrade response using chunked encoding:

Following is the response packet that indicates switching to websocket:

HTTP/1.1 101 Switching Protocols
Date: Thu, 10 Apr 2014 15:21:28 GMT
Server: Apache/2.2.15 (CentOS)
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: S7wgnmtVSahQDCifkYwFlQajWcI=
X-Powered-By: Phusion Passenger 4.0.41
Status: 101 Switching Protocols
Transfer-Encoding: chunked
Content-Type: text/plain; charset=UTF-8

5
..1::

The problem is that right after the http headers the server (nodejs) sends actual websocket frame but apache encodes it using chunked encoding (prepends it with chunk length - the number 5 in my example). Since the first byte of websocket frame is supposed to be frame opcode the chunk length is taken by browser (tested with Chrome) as this opcode and whole websocket is corrupted.

What am I doing wrong?

Thanks,
Antony.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.