npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

snooze-socket

v1.0.2

Published

Entities for using SocketIO in SnoozeJS

Downloads

4

Readme

snooze-socket

Entities for using SocketIO in SnoozeJS

Installation

Install from NPM.

    npm install snooze-socket

Inject the snooze-socket module in your app module. snooze-socket depends on snooze-baselib and snooze-controller. These will automatically get imported when you import snooze-socket.

    snooze.module('myApp', ['snooze-socket']);

Use

You can create a socket by giving a port or setting the http handler (like Express).

(function() {
    'use strict';
    
    snooze.module('myApp', ['snooze-socket'])
        .socket('ChatSocket', {
            port: 9876
        })
        .run(function(ChatSocket) {
            ChatSocket.on('message', function() {});
        });
});

The socket constructor takes several properties defining the socket. port, handler, controllers, and namespaces. To set a handler, pass the http app object to the handler property:

var app = require('express')();
var server = require('http').Server(app);

server.listen(80);

snooze.module('myApp', ['snooze-socket'])
    .socket('ChatSocket', {
        handler: server
    });
    

You can use the named socket (in this case ChatSocket) as an injectable, however, using the snooze-controller is recommended.

Controllers

Using the snooze-controller we can easily link socket events to controller methods.

snooze.module('myApp', ['snooze-socket'])
    .controller('ChatCtrl', {
        'message': function($opts) {
            var message = $opts.data.message;
            $opts.client.emit('response', {message: 'You sent: ' + message});
            $opts.socket.emit('response', {message: 'Hello EVERYONE'});
        }
    })
    .socket('ChatSocket', {
        port: 9876,
        controllers: ['ChatCtrl']
    });
    

After Entities are compiled the socket will go through a post-compile phase that binds controller methods to socket events. Multiple controllers can be assigned to a socket to split responsibility and create better organization.

snooze.module('myApp', ['snooze-socket'])
    .controller('UserCtrl', {
        'connection': function($opts, UserManager) {
            UserManager.loginUser($opts.client);
        },
        'disconnect': function($opts, UserManager) {
            UserManager.logoutUser($opts.client);
        }
    })
    .controller('ChatCtrl', {
        'message': function($opts, ChatManager) {
            var message = $opts.data.message;
            ChatManager.storeMessage(message);
        }
    })
    .socket('ChatSocket', {
        port: 9876,
        controllers: ['ChatCtrl', 'UserCtrl']
    });
    

In the above example socket.on('connection') and socket.on('disconnect') events will pass to the UserCtrl.connection and UserCtrl.disconnect methods. The socket.on('message') event will pass to the ChatCtrl.message method.

$opts

The $opts injectable passed to the Controller contains the following properties:

  • socket - The Socket object created through module.socket. Use socket.emit to send a message to all clients.
  • client - The Socket instance the generated the event. Use client.emit to send a message to that specific client.
  • data - The data recieved for the event.

Namespaces

socket.io Namespaces are supported. Each namespace acts like a socket itself. To set a namespaces use the namespaces property in the socket constructor.

snooze.module('myApp', ['snooze-socket'])
    .socket('ChatSocket', {
        port: 9876,
        namespaces: {
            '/chat': {
                controllers: ['ChatCtrl']
            },
            '/users': {
                controller: ['UsersCtrl']
            }
        }
    });
    

This example creates two namespaces. One at http://localhost:9876/chat and one at http://localhost:9876/users. For more information on namespaces see socket.io namespaces.

Demo

See the socket.io chat room demo. Then see the snooze-socket chat room demo. See how using snooze organization makes setting up a socket.io application easier.

Contact

We're devoted to making snooze-socket and snooze better. Please contact us with comments/questions/bugs and we will address them ASAP.