Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Connect an Angular drawing app to a Vert.x backend by serving the single-page application from Vert.x, exposing a SockJS endpoint through the Vert.x event-bus bridge, and handling drawing commands with event-bus consumers. SockJS gives the browser a WebSocket-like API with fallback transports; the bridge lets browser code send, publish, and receive messages on named event-bus addresses.

How the Angular and Vert.x drawing example fits together

The browser and server communicate through a SockJS endpoint at /eventbus/*. Vert.x routes that endpoint through a SockJS handler configured as an event-bus bridge. Angular loads as a single-page application from the same Vert.x HTTP server, while server-side consumers process drawing and guest commands.

  • HTTP router: directs event-bus traffic to the SockJS handler and application requests to static-file handling.
  • Angular app: runs in the browser and uses the JavaScript event-bus client.
  • Event-bus consumers: listen for drawing commands at Commands.newDrawing and Commands.newDrawGuest.
  • Shared data: keeps drawing-session state in a Vert.x local shared-data map.

The sample implementation is available in the Vert.x examples repository. The connection is not simply a raw WebSocket carrying drawing data: the bridge maps browser operations onto the Vert.x event bus.

What WebSockets, SockJS, and the event-bus bridge each do

WebSockets provide the persistent, full-duplex connection

Eclipse Vert.x describes WebSockets as “a web technology that allows a full duplex socket-like connection between HTTP servers and HTTP clients (typically browsers).” Vert.x supports WebSockets on both the server and client sides. Server code can write text with writeTextMessage or binary data with writeBinaryMessage; messages beyond the configured maximum frame size can be split into multiple frames. See the Vert.x Core WebSockets documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

SockJS supplies a browser-facing API and transport fallback

SockJS presents a WebSocket-like interface to browser JavaScript and chooses among available transports based on browser and network capability. This can provide a fallback when a native WebSocket connection is unavailable. The bridge uses the client-side vertx-eventbus.js library to connect browser code to Vert.x event-bus operations. See the Vert.x-Web SockJS documentation.

The bridge adds named message routing

A raw WebSocket gives application code a direct socket through which it defines its own message protocol and routing. The Vert.x event-bus bridge instead exposes event-bus operations to the browser: client code can register handlers, send messages to an address, or publish messages to an address. Server-side consumers can then handle messages at addresses such as Commands.newDrawing. Bridge events can be inspected and rejected, allowing the server to govern which addresses and operations browser clients may use.

Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Serve the Angular single-page app from Vert.x

Configure the HTTP router so the event-bus endpoint is handled before static application content. The example disables static caching, sets /index.html as the index page, and reroutes client-side application paths to that entry point. The route order matters: requests under /eventbus/* must reach the SockJS handler rather than the static handler.

  1. Register the SockJS bridge route: configure router.route("/eventbus/*").handler(sockJSHandler).
  2. Configure static serving: use StaticHandler.create().setCachingEnabled(false).setIndexPage("/index.html") to serve the Angular build files.
  3. Route Angular paths to its entry page: add a regex route that reroutes client-side paths to /index.html. This lets Angular’s router handle paths that do not correspond to separate server files.
  4. Start the HTTP server: pass the router to vertx.createHttpServer().requestHandler(router).listen(...).

Disabling caching is the sample’s explicit configuration; production cache policy should be chosen deliberately for the deployed Angular assets rather than copied without considering update behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Connect Angular to the Vert.x event bus

Load the Vert.x event-bus JavaScript client in the Angular application and connect it to the bridge endpoint. The client can register a handler for an address, send a message to an address, or publish a message. On the server, register consumers for the addresses the application intends to support.

For the drawing example, the server registers consumers for Commands.newDrawing and Commands.newDrawGuest. The Angular client should send the corresponding command to the intended address, and register a handler on any address from which it expects updates. A send routes a message to an address; publish is the event-bus operation to use when the application intends to distribute a message to subscribers. The bridge therefore avoids making the browser implement its own socket-level routing protocol, but it also makes bridge permissions an essential part of the design.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Keep drawing-session state in shared data

The example obtains session state with vertx.sharedData().getLocalMap(id) and reads the stored drawState. This local map provides shared data within the Vert.x instance for the sample’s drawing-session workflow. The presence of a local map does not by itself establish durable persistence or cross-instance state sharing; deployments that need those properties require an appropriate storage or coordination design beyond the behavior described here.

Choose raw WebSockets or the SockJS bridge

Consideration Raw WebSocket SockJS with the Vert.x event-bus bridge
Browser and network fallback Uses the WebSocket connection directly; no SockJS transport selection is part of the raw approach. SockJS can select fallback transports according to browser and network capability.
Message routing Application defines its own direct socket message protocol and routing. Browser operations map to named event-bus addresses, including send, publish, and register/receive.
Authorization granularity Application must implement its own message-level authorization and routing rules. Bridge events can be inspected and rejected, and rules can govern permitted addresses and operations.
Operational complexity Requires managing the socket protocol and its routing in application code. Requires configuring the bridge, client library, and bridge permissions in addition to event-bus consumers.
Angular client fit Angular can use a browser WebSocket, but the application must implement its own message handling. The Vert.x event-bus JavaScript client offers event-bus operations to browser code.

For an application already organized around Vert.x event-bus addresses, the bridge provides a natural browser integration and can simplify message routing. Choose a raw WebSocket when direct socket semantics and a custom protocol are the better fit. SockJS fallback behavior is useful when browser or network support varies, but it does not remove the need to secure the endpoint.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Secure the SockJS bridge before exposing it

WebSockets are not constrained by the browser’s same-origin policy. An internet-facing SockJS bridge should therefore set an allowed origin and enforce authorization rules rather than assuming that browser origin behavior will protect event-bus destinations. The Vert.x-Web guide recommends configuring the origin on the SockJS handler to reduce cross-site WebSocket hijacking risk. It also describes inspecting and rejecting bridge events and configuring permitted addresses and authorization rules. Consult the Vert.x-Web SockJS and bridge guidance before exposing the endpoint.

  • Set the allowed origin to the intended internet-facing application origin.
  • Permit only the event-bus addresses and operations required by the browser.
  • Use bridge event inspection and rejection rules to apply authorization before forwarding messages.
  • Do not treat an obscure endpoint path or a successful SockJS connection as authentication.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.