Giter Club home page Giter Club logo

Comments (3)

half-metal avatar half-metal commented on May 24, 2024

I also tried this based on what I read in the discussions
Server.publish('locationHb', 'you are now subscribed to locationHb')
But this also doesn't seem to send the message

from hyper-express.

kartikk221 avatar kartikk221 commented on May 24, 2024

Hi, so MQTT is essentially a URL protocol which determines how Publish/Subscribing will work between multiple parties.

The reason why your example is not working is because connections must first subscribe to a topic using the Websocket.subscribe(topic) method.

Then whenever you want to publish a message to all connections on a Server instance, you can simply do Server.publish(topic, message) to broadcast a message.

Below is a simple example of this:

const crypto = require('crypto');
const HyperExpress = require('hyper-express');
const Server = new HyperExpress.Server();

// Create an endpoint for accepting websocket connections
Server.ws('/connect', (ws) => {
    // Attach a unique identifier to each connection
    ws.id = crypto.randomUUID();
    
    // Subscribe the connection to "OUR_TOPIC_NAME"
    ws.subscribe('OUR_TOPIC_NAME');
    console.log(`Websocket Connection ${ws.id} Is Now Connected & Subscribed To OUR_TOPIC_NAME!`);
    
    // Bind any handlers here for this connection, we will bind the close handler so we can log closure
    ws.on('close', (code, message) => {
        console.log(`Websocket Connection ${ws.id} Has Disconnected!`);
    });
});

// Now you can publish a message to all connections over the "OUR_TOPIC_NAME" topic like below
Server.publish('OUR_TOPIC_NAME', 'This is a message for OUR_TOPIC_NAME subscribers!');

I'll also be adding an example for MQTT Pub/Sub in the Examples section of the documentation in the next update.

Hope this helps!

from hyper-express.

half-metal avatar half-metal commented on May 24, 2024

awesome, that works, I appreciate the example. Yes, I needed to have ws.subscribe('topic') instead of ws.subscribe = 'topic' and as you showed there Server.publish - Thank you!

from hyper-express.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.