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.


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://", {params: {token: "your-token"}})


// Now that you are connected, you can join channels with a topic:
let channel ="room:123", {})
.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