LogoLogo
ChangelogGitHubTwitterGitter
v2.x
v2.x
  • Introduction
  • Overview
    • Getting started
    • Effects
    • Routing
    • Middlewares
    • Error handling
    • How does it glue​ together?
  • Advanced
    • Context
    • Server events
    • Validation
    • Streaming
    • Output interceptor
  • WebSockets
    • Getting started
    • Effects
    • Middlewares
    • Error handling
    • Connections handling
  • API Reference
    • core
      • bindTo
      • createServer
      • combineRoutes
      • createContextToken
      • EffectFactory
      • r.pipe
      • httpListener
      • operator: matchEvent
      • operator: use
    • websockets
      • webSocketListener
      • operator: broadcast
      • operator: mapToServer
    • middleware-body
    • middleware-logger
    • middleware-io
    • middleware-jwt
      • Token signing
    • middleware-joi
    • middleware-cors
    • middleware-multipart
  • Other
    • Migration from version 1.x
    • Changelog
    • FAQ
Powered by GitBook
On this page
  1. WebSockets

Connections handling

The WebSocket protocol doesn’t handle authorization or authentication. Practically, this means that a WebSocket opened from a page behind auth doesn’t “automatically” receive any sort of auth; you need to take steps to also secure the WebSocket connection.

This can be done in a variety of ways, as WebSockets will pass through standard HTTP headers commonly used for authentication. This means you could use the same authentication mechanism you’re using on WebSocket connections as well.

The first possible solution assumes that the client will be authenticated during initial WebSocket connection. The webSocketListener config exposes a connection$ stream of incoming connections that the developer can listen to and do some action on it. For example we can validate the incoming request headers and check for JWT token validity. In negative scenario we should throw an connection error with reason why it failed, and in positive scenario we should just pass the request through.

import {
  webSocketListener,
  WebSocketConnectionError,
  WsConnectionEffect,
} from '@marblejs/websockets';

const connection$: WsConnectionEffect = req$ =>
  req$.pipe(
    mergeMap(req => iif(
      () => !isAuthorized(req)
      throwError(new WebSocketConnectionError('Unauthorized', 4000)),
      of(req),
    )),
  );
  
export const webSocketServer = webSocketListener({
  middlewares,
  effects,
  connection$,
});
PreviousError handlingNextcore

Last updated 6 years ago