HTTP Servers

Learn how to use a webserver together with deepstream

deepstream is a realtime data server and a great one of that. It is not a general purpose webserver though. That means that if you’re building a web application that needs to serve HTML or CSS files, images etc., you’ll also need a classic HTTP server. For that, you have a number of choices

Using Nginx, Apache, Tomcat, IIS etc.

You can use any established webserver together with deepstream. Just make sure that the URL path to route HTTP/WS traffic (by default yourdomain.com/deepstream) is proxied forward.

You can change this path by setting urlPath in the server config and path in the client options to a different value.

Here’s an example of what the proxy configuration would look like for Nginx. To learn more about how to use Nginx as a reverse proxy and load balancer for deepstream, head over to the Nginx Tutorial

# in the http{} configuration block
upstream deepstream {
    ip_hash;
    server localhost:6020;
    # add more servers here for loadbalancing
}

server {
    server_name app.domain.com;
    listen 80;
    location /deepstream {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://deepstream;
    }
}

Using a CDN / static file hosting service

Especially for larger deployments it can make perfect sense to keep your server logic in deepstream and serve assets via a static file host, fronted by a Content Delivery Network (CDN). Similar to the section above, all that’s necessary here is to exclude the /deepstream path from the CDN. Depending on your CDN of choice, this can be a bit tricky though. AWS Cloudfront for instance only allows proxying of HTTP traffic, so deepstream traffic needs to be re-routed on an Elastic Load Balancer level, more about this here. Other CDNs like CloudFlare support socket traffic directly, more about this here.

Run via your own node server

Deepstream doesn’t expose the built in HTTP server to users, but you can opt to provide your own simple by doing the following:

const { Deepstream } = require('./dist/src/deepstream.io')
const { Server } = require('http')

const httpServer = new Server()

const ds = new Deepstream({
    connectionEndpoints: [{ 
        type: 'ws-websocket',
        options: { 
            httpServer
        } 
    }]
})
ds.start()

httpServer.listen(6020, function(){
    console.log( 'HTTP server listening on 6020' );
})