Hapi and Support of HTTP/2 #2510

Closed
jmorille opened this Issue Apr 21, 2015 · 9 comments

Comments

Projects
None yet
8 participants
@jmorille

Hello,

Does Hapy have any support of HTTP/2 in natif or by plugins ?

Thank by advance

@hueniverse

This comment has been minimized.

Show comment
Hide comment
@hueniverse

hueniverse Apr 21, 2015

Member

You can provide your own server implementation when creating a connection using the listener option https://github.com/hapijs/hapi/blob/master/API.md#serverconnectionoptions.

Member

hueniverse commented Apr 21, 2015

You can provide your own server implementation when creating a connection using the listener option https://github.com/hapijs/hapi/blob/master/API.md#serverconnectionoptions.

@hueniverse hueniverse closed this Apr 21, 2015

@hueniverse hueniverse added the question label Apr 21, 2015

@hueniverse hueniverse self-assigned this Apr 21, 2015

@AdriVanHoudt

This comment has been minimized.

Show comment
Hide comment
@AdriVanHoudt

AdriVanHoudt Apr 21, 2015

Member

This comes down to: you can provide your own http2 node server implementation until node/iojs implements it right?

Member

AdriVanHoudt commented Apr 21, 2015

This comes down to: you can provide your own http2 node server implementation until node/iojs implements it right?

@jmorille

This comment has been minimized.

Show comment
Hide comment
@jmorille

jmorille May 7, 2015

it is working like express ?

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('hello, http2!')
})

var options = {
  key: fs.readFileSync('./example/localhost.key'),
  cert: fs.readFileSync('./example/localhost.crt')
};

require('http2').createServer(options, app).listen(8080);

jmorille commented May 7, 2015

it is working like express ?

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('hello, http2!')
})

var options = {
  key: fs.readFileSync('./example/localhost.key'),
  cert: fs.readFileSync('./example/localhost.crt')
};

require('http2').createServer(options, app).listen(8080);
@jmorille

This comment has been minimized.

Show comment
Hide comment
@jmorille

jmorille May 7, 2015

It not work like as if I looked the socket.io sample
http://matt-harrison.com/using-hapi-js-with-socket-io/

I will try it with node-http2 module..

jmorille commented May 7, 2015

It not work like as if I looked the socket.io sample
http://matt-harrison.com/using-hapi-js-with-socket-io/

I will try it with node-http2 module..

@peterhanneman

This comment has been minimized.

Show comment
Hide comment
@peterhanneman

peterhanneman Jun 5, 2015

I couldn't find any resources online with a direct explanation of how to implement HTTP2 with Hapi so I'll document my solution here:

var Fs = require('fs');
var Hapi = require('hapi');
var Http2 = require('http2');

var options = {
  key: Fs.readFileSync('./ssl/site.key');
  cert: Fs.readFileSync('./ssl/site.crt');
};

var server = new Hapi.Server();
server.connection({
   listener: Http2.createServer(options),
   host: 'localhost',
   port: 8080,
   tls: true
});

It would appear that Socket.io is NOT yet compatible with the HTTP2 library. I've submitted a bug report. The workaround is serving Socket.io from another connection with the usual HTTPS listener.

I couldn't find any resources online with a direct explanation of how to implement HTTP2 with Hapi so I'll document my solution here:

var Fs = require('fs');
var Hapi = require('hapi');
var Http2 = require('http2');

var options = {
  key: Fs.readFileSync('./ssl/site.key');
  cert: Fs.readFileSync('./ssl/site.crt');
};

var server = new Hapi.Server();
server.connection({
   listener: Http2.createServer(options),
   host: 'localhost',
   port: 8080,
   tls: true
});

It would appear that Socket.io is NOT yet compatible with the HTTP2 library. I've submitted a bug report. The workaround is serving Socket.io from another connection with the usual HTTPS listener.

@casalot

This comment has been minimized.

Show comment
Hide comment
@casalot

casalot Jan 15, 2016

@timelessvirtues, I've tried your sample above, but I keep getting a self.listener.address is not a function. Any ideas?

casalot commented Jan 15, 2016

@timelessvirtues, I've tried your sample above, but I keep getting a self.listener.address is not a function. Any ideas?

@mikefrey

This comment has been minimized.

Show comment
Hide comment
@mikefrey

mikefrey Jan 20, 2016

Contributor

@casalot I had luck doing this:

'use strict'
const fs = require('fs')
const Hapi = require('hapi')
const Http2 = require('http2')
const server = new Hapi.Server()

let listener = Http2.createServer({
  key: fs.readFileSync('./key.pem'),
  cert: fs.readFileSync('./cert.pem')
})

if (!listener.address) {
  listener.address = function() {
    return this._server.address()
  }
}

server.connection({
  listener: listener,
  port: '8000',
  tls: true
})

server.route({
  method: 'GET',
  path: '/',
  handler: (request, reply) => reply('hello world')
})

server.start(err => {
  if (err) console.error(err)
  console.log(`Started ${server.connections.length} connections`)
})
Contributor

mikefrey commented Jan 20, 2016

@casalot I had luck doing this:

'use strict'
const fs = require('fs')
const Hapi = require('hapi')
const Http2 = require('http2')
const server = new Hapi.Server()

let listener = Http2.createServer({
  key: fs.readFileSync('./key.pem'),
  cert: fs.readFileSync('./cert.pem')
})

if (!listener.address) {
  listener.address = function() {
    return this._server.address()
  }
}

server.connection({
  listener: listener,
  port: '8000',
  tls: true
})

server.route({
  method: 'GET',
  path: '/',
  handler: (request, reply) => reply('hello world')
})

server.start(err => {
  if (err) console.error(err)
  console.log(`Started ${server.connections.length} connections`)
})
@penumakauday

This comment has been minimized.

Show comment
Hide comment
@penumakauday

penumakauday Mar 18, 2016

Hi, I am trying to add the https config tls to manifest js as below

connections: [{
host: {
$filter: 'env',
production: process.env.HOST,
$default: '0.0.0.0'
},
port: {
$filter: 'env',
production: process.env.PORT,
$default: 3000

},
tls:{
key : fs.readFileSync('testkey.pem'),
cert : fs.readFileSync('testcert.pem')
}}]

But https server is not getting started. Could anyone help me on fixing this.

Hi, I am trying to add the https config tls to manifest js as below

connections: [{
host: {
$filter: 'env',
production: process.env.HOST,
$default: '0.0.0.0'
},
port: {
$filter: 'env',
production: process.env.PORT,
$default: 3000

},
tls:{
key : fs.readFileSync('testkey.pem'),
cert : fs.readFileSync('testcert.pem')
}}]

But https server is not getting started. Could anyone help me on fixing this.

@Marsup

This comment has been minimized.

Show comment
Hide comment
@Marsup

Marsup Mar 18, 2016

Member

Please avoid hijacking unrelated threads when you already have yours.

Member

Marsup commented Mar 18, 2016

Please avoid hijacking unrelated threads when you already have yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment