socketio/engine.io-client

Node.js (typescript) with extraHeaders

majordo opened this issue · 5 comments

  • report a bug
  • request a feature

Current behaviour

I've a socket.io-client running in a nodejs back-end and connecting to a socket.io server in another machine. I need the client to use the extraHeaders option but the typescript compiler return an error:
"Object literal may only specify known properties, and 'extraHeaders' does not exist in type 'ConnectOpts'"

const socket = io(
    "/",
    {
        extraHeaders: { "x-origin": "......" }
    });

The decumentation says extraHeaders is a node.js-only option, so what's wrong?
Thanks in advance

PS Without typescript it's working as supposed

Setup

  • OS: macOS
  • browser: node.js / typescript
  • engine.io version: 3.0.4

Hi! I am not able to reproduce: https://github.com/socketio/socket.io/tree/master/examples/typescript

import { io } from "socket.io-client";

const socket = io("ws://localhost:8080/", {
    extraHeaders: {
        test: "123"
    }
});

Could you please check?

This is my package.json

{
 ...
  "devDependencies": {
    "@types/express": "^4.17.9",
    "@types/node": "^14.14.16",
    "@types/socket.io-client": "^1.4.34",
    "nodemon": "^2.0.6",
    "ts-node": "^9.1.1",
    "tslint": "^6.0.0",
    "typescript": "^4.1.3"
  },
  "dependencies": {
    ...
    "express": "^4.17.1",
    "socket.io-client": "^3.0.4",
    ....
  }
}

If I try to import io this way:
import { io } from 'socket.io-client';

I've got a new error:

src/app.ts:12:10 - error TS2305: Module '"socket.io-client"' has no exported member 'io'.

I think you can remove the @types/socket.io-client dependency, as the codebase is now written in TypeScript and the definitions are included in the socket.io-client package.

Could you please check if this fixes your issue?

You are right!
Removing "@ types / socket.io-client" fixed the problem.

Thanks