adamhalasz/diet

Better Implementation of Protocol Handling

Opened this issue · 11 comments

We should discuss here how to modify diet.js to handle simply all protocols via the current protocol/name shema

--- needed Modifications

  • app.protocol looks in /app/protocols/name first then in dietjs so we can overwrite the create server method to return any server we whant to have (net for tcp)

maybe a method for registring protocol handlers by name in the app object as like

app.protocolHandler('http', '/app/protocol/http.js')
app.listen('http://host.tld')

i use such functionality in my fork already but its badly hacked code for example i implamented proxy://
and lb:// protocol with costum http servers that do the job like the name says but i also did mysql:// and other protocols a lot

Hey @frank-dspeed I like this idea a lot. I redesigned diet a while ago specifically with this in mind. That's why we have the protocols folder. I will add WebSocket support soon. If you have a Proposal and working code send me a link and I will look over it.

The Protocols should be Third Party modules by the way and you could attach them like this:

// server.js
var server = require('diet')
var app = server()

// load websocket protocol 
var io = require('diet-protocol-websocket')
    io.listen(3000)

// attach websocket protocol to diet
app.protocol(io)

// listen on port 8000
app.listen(8000)

Then the Method Listeners will become universal and accept both HTTP and WebSocket requests on the same routes sharing the same API.

// routes.js
app.post('/send', function($){
     $.end('hello', $.body.name)
})

From the Client you could send data with socket.io simply like this to the GET /send route:

// main.js
var socket = io()
socket.send({
     method: 'post',
     body: {
          name: 'John Doe'
     }
})

Thanks,
Adam

good proposal but we need to do listen by protocol where some protocols can do multi listen and some need to fail when anything listens already.

askie commented

It's wonderful when diet support socket.io or IPC protocol of router,
diet will adapt to develope a electron app!

@askie i think we have socket.io support at present and also a feathers integration

askie commented

@frank-dspeed how to use socket.io and use the same router of diet?
such as:
server code:

var server = require('diet')
var app = server()
app.listen(8000)

var io = require('socket.io')(app.server) // <-- use app.server 
io.on('connection', function(socket){
    console.log(' ... socket.io: A USER CONNECTED');
});

app.get('/api/get', function($){
    $.data.name="john";
    $.json();
});`

client code:

var io = io();
io.on('connection', function(socket){
  socket.emit('/api/get',function(a){
  console.log(a) //   {name:"john"}
} ); 
});

/api/get is defined by the diet

is this adapt?

@askie thats a totall diffrent issue you open a new 👍 for that kind of questions this is a Proposal for something more advanced.

@askie @askie thats a totall diffrent issue you open a new 👍 for that kind of questions this is a Proposal for something more advanced.

Related #47