Skip to content
This repository has been archived by the owner on Nov 2, 2021. It is now read-only.
/ azure-websockets Public archive

A personal edition of azure-websockets module

Notifications You must be signed in to change notification settings

vicancy/azure-websockets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Usage:

const { WebPubSubServer } = require('./webpubsub')
const dotenv = require('dotenv');
const http = require('http');

dotenv.config();

const wpsserver = new WebPubSubServer(process.env.WPS_CONNECTION_STRING, 'chat');

// sample of client negotiate
console.log(wpsserver.endpoint.clientNegotiate('chat', {
  userId: "vicancy",
  claims: {
    hey: ["w"],
    role: ["webpubsub.group.join"],
 }
}));

const serviceClient = wpsserver.createServiceClient();
const handler = wpsserver.createCloudEventsHandler(
  {
    //path: "/customUrl", // optional
    onConnect: async connectRequest => {
      return {
        userId: "vicancy"
      };
    },
    onConnected: async connectedRequest => {
      await serviceClient.sendToAll(connectedRequest.context.connectionId + " connected");
    },
    onUserEvent: async userRequest => {
      console.log(`Received user request data: ${userRequest.payload.data}`);
      if (userRequest.payload.data === 'abort') {
        return {
          error: {
            detail: "aborted"
          }
        };
      }
      if (userRequest.payload.data === 'error') {
        throw new Error("error from inside the event");
      }
      return {
        payload: {
          data: "Hey " + userRequest.payload.data,
          dataType: userRequest.payload.dataType
        }
      };
    },
    onDisconnected: async disconnectRequest => {
      console.log(disconnectRequest.context.userId + " disconnected");
    }
  }
);

const port = 3000;

const server = http.createServer(async (request, response) => {
  if (!await handler.handleRequest(request, response)) {
    console.log(`${request.url} for others to process`);
    response.statusCode = 404;
    response.end();
  }
});

server.listen(port, () => console.log(`Azure WebPubSub Upstream ready at http://localhost:${port}${handler.path}`));

About

A personal edition of azure-websockets module

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published