Pub/Sub Node.js environment

Publish-Subscribe Messaging Pattern with Phoenix PubSub Adapter:

The Publish-Subscribe messaging pattern operates by enabling a group of consumers to subscribe to events based on specific topics. When an event is published into a topic channel, copies of the message are delivered to each subscriber, allowing for seamless communication without direct knowledge between producers and consumers.

In the context of Phoenix PubSub, which is based on pg , it operates on Distributed Erlang nodes following the principles of the CAP theorem. The underlying pg system is AP (Available and Partition Tolerant), ensuring availability even in the face of network partitions.

This adapter provides a seamless solution for sending and receiving Pub/Sub messages in Node.js applications. Notably, the Server version of the Phoenix channel allows for server-side channel joining. By subscribing to a set of "topics," applications can efficiently obtain messages published under those topics. This decoupling of producers and consumers enhances system flexibility and scalability.

This is only intended for back-end applications running on Node.js.

Installation

The first step is to install the Phonenix channel library by running the following command in your project directory using NPM.

npm i phoenix-channels


const { Socket } = require('phoenix-channels')
let socket = new Socket("wss://api.sariska.io/api/v1/messaging/websocket", {params: {token: "your-token"}})

socket.connect()

// Now that you are connected, you can join channels with a topic:
let channel = socket.channel("room:123", {})
channel.join()
.receive("ok", resp => { console.log("Joined successfully", resp) })
.receive("error", resp => { console.log("Unable to join", resp) })

Note: The rest of the steps are the same as the Real-Time Messaging SDK development.

Last updated