Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Creating a websocket server using Typescript #487

Closed
briant-spindance opened this issue May 14, 2020 · 7 comments
Closed

[question] Creating a websocket server using Typescript #487

briant-spindance opened this issue May 14, 2020 · 7 comments
Labels

Comments

@briant-spindance
Copy link

I'm trying to make a create a websocket server in Typescript, and I can't seem to translate the Javascript examples I've found into Typescript.

Here's what I have so far:

import { Server } from "aedes";
import * as http from "http";
import * as ws from "websocket-stream";

const broker = Server();
const httpServer = http.createServer();
ws.createServer({ server: httpServer }, broker.handle);

I'm getting an error:

"Argument of type '(stream: Connection) => Client' is not assignable to parameter of type '() => void'."

Any pointers on where I'm going wrong?

@robertsLando
Copy link
Member

robertsLando commented May 14, 2020

The problem is in websocket-stream module interface: https://github.com/maxogden/websocket-stream/blob/master/index.d.ts#L21

That type mismatch with the one on Aedes.d.ts

However you can also use another way:

const { createWebSocketStream, Server } = require('ws')

function createWebsocketServer (server, broker) {
  const ws = new Server({ server })

  ws.on('connection', function (conn, req) {
    var stream = createWebSocketStream(conn)
    broker.handle(stream)
  })
}

const http = require('http')
const server = http.createServer()
// Than use this function to bind aedes to your server
createWebsocketServer(server, broker)

@briant-spindance
Copy link
Author

Sorry, looks like I +1 too quick!

 stream._socket = conn._socket

throws an error, and it looks like broker.handle only accepts one param.

BTW, if we can get this sorted out, I'd be happy to open a PR for some better Typescript examples.

@robertsLando robertsLando reopened this May 15, 2020
@robertsLando
Copy link
Member

@briant-spindance Try with my updated comment now and let me know if it works

@briant-spindance
Copy link
Author

@robertsLando That worked! Thanks for the help.

@joseAveleira
Copy link

joseAveleira commented May 22, 2020

Sorry, I have a similar problem, I'm trying to make a project in typescript with MQTT, express and websockets functionality but I don't know how to implement the solution mentioned in the previous comment

import express from "express";
import { Server, Client, AuthenticateError } from "aedes";
import { createServer, Socket } from "net";
import * as http from "http";
//import * as ws from "websocket-stream"  //problem "Argument of type '(stream: Connection) => Client' is not assignable to parameter of type '() => void'."
var ws = require("websocket-stream"); 

const broker = Server({
  heartbeatInterval: 60000,
});

broker.on("publish", (packet, client) => {
  if (client) {
    console.log("%s : topic %s : %s", client.id, packet.topic, packet.payload);
  }
});

// Initializations
const app = express();

// Settings
app.set("port", process.env.PORT || 3000);

//ws server

/* Problem  createWebSocketStream is not a function
import { createWebSocketStream, Server } from 'ws';
const serverws = http.createServer();

const ws = new Server({server:serverws, port:2000})
ws.on('connection', function (conn, req) {
  var stream = createWebSocketStream(conn)
  broker.handle(stream)
})
*/

var wsServer = http.createServer();
ws.createServer({ server: wsServer }, broker.handle);
wsServer.listen(2000, function () {
  console.log("functional WS");
});

// MQTT server
const server = createServer(broker.handle);
server.listen(1883);

//test Express
app.get("/", function (req, res) {
  res.send("Hello Worlds!");
});

//Express server
app.listen(app.get("port"), () => {
  console.log(`server on port ${app.get("port")}`);
});

`

@robertsLando
Copy link
Member

robertsLando commented May 25, 2020

npm install --save ws ?

@joseAveleira
Copy link

Yes, thank you, it was a bit frustrating, I installed WS and it didn't fail to find the module, but when I reinstalled it, it was fixed

Thank you very much, I'll keep working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants