Connecting to websocket channels after successful connection

1.6k Views Asked by At

I have been working on a websocket client application.

I am currently using the ws client library, because it is easy to add some headers (I need this for authentication purposes). I have made a successful connection to the server, but now I need to connect to a specific channel.

Current code:

const WebSocket = require('ws');

var option = {
  headers: {
    api_key: 'xxxxx'
  }
};

// I know this is not a correct url, but I removed it for security reasons
const url = '../websocket/'; 

const wss = new WebSocket(url, option);


wss.on('open', () => {

  console.log("Connection is succesfull");

  wss.on('message', message => {

    console.log(message);
  });

});

When I ran the code it prints the "Connection succesfull", but now I want to connect to a channel called /people. How can I do this.

I have tried several things like:

Changed the url websocket/people.

This doesn't work because it first needs to authenticate to user, before making a connection to a channel

Changed the url to websocket/?people.

I don't get an error, but I also don't get response back when something is send to this channel.

Add this in the open function:

 wss.on('/people', message => {

        console.log(message);
 });

I don't get an error, but I also don't get response back when something is send to this channel.

For the record. I only have access to the documentation and not to the server.

0

There are 0 best solutions below