question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending custom events to client

See original GitHub issue

Hi guys!

Maybe i can`t find it, but is there a way to send a custom event to a page and listen to it via js?

I have to update a variable after a file has been changed.

Thanks in advance. Greets

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
shakyShanecommented, Jun 9, 2015

It’s a little bit wonky right now as we’re still looking at improving this use case - but the following will work.

var bs = require('browser-sync').create();

/**
 * Start BrowserSync as normal
 */
bs.init({
    server: 'test/fixtures',
    plugins: [
        // create plugin to load your JS onto clients
        {
            plugin: function () {}, // required for plugin system :(
            hooks: {
                "client:js": require('fs').readFileSync('test/fixtures/some.js', 'utf-8')
            }
        }
    ]
}, function (err, instance) {

    /**
     * Your custom watcher for all files with test/fixtures directory
     */
    bs.watch("test/fixtures").on('change', function (file) {

        /**
         * Emit custom event to clients
         */
        instance.io.sockets.emit('custom-event', {file: file});
    });
});

then, the contents of some.js would be something like

;(function (bs) {

    bs.socket.on('custom-event', function (data) {
        console.log(data);
    })

})(___browserSync___);
1reaction
shakyShanecommented, Jun 10, 2015

No problem. I’m going to work on the api for this use-case, I’m thinking my above example could look like:

var bs = require('browser-sync').create();

// register some client-side JS (without needing plugin boilerplate from above)
bs.addClientJs(somefile);

// start Browsersync
bs.init({server: './app'});

// expose 'sockets' to public API to allow:
bs.watch('**/*.js').on('change, function(file) {
    bs.sockets.emit('custom-event', {file: file});
});

or, should you just want to access window.___browserSync___ in your own scripts, you should be able to disable the async attribute on the snippet.

bs.init({
    server: './app',
    snippetOptions: {
        async: false
    }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sending custom events - LaunchDarkly docs
This topic explains how to send custom events using the track feature in your SDK. The track feature is available on both client-side...
Read more >
Send custom events - Amazon CloudWatch
There are two ways to record custom events in the CloudWatch RUM web client. Use the CloudWatch RUM web client's recordEvent API. Use...
Read more >
Custom Events - Other/REST chat - GetStream.io
This allows you to send custom events to a connected user. The event is delivered to all connected clients for that user. It...
Read more >
FS.event API - Sending custom event data into FullStory
You can send client-side instrumented events into FullStory via our Javascript API to search and segment your sessions. Screen Shot 2022-06-17 at 10.44.50...
Read more >
Custom Events - Vonage Developer
Custom events allow you to add custom metadata to conversations by recording data alongside your text or audio events. You can add events...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found