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

@ericlathrop/phoenix-js-react-hooks

v0.1.0

Published

[React](https://reactjs.org/) [hooks](https://reactjs.org/docs/hooks-reference.html) for working with [Phoenix's](https://www.phoenixframework.org/) [channels](https://hexdocs.pm/phoenix/channels.html).

Downloads

82

Readme

phoenix-js-react-hooks

React hooks for working with Phoenix's channels.

Usage

Wrap the top of your app with a <SocketProvider> so child components will have access to the socket.

import { SocketProvider } from '@ericlathrop/phoenix-js-react-hooks';

function App() {
  const socketUrl = "wss://localhost:4000/socket";
  const socketOptions = { params: { 'user_id': userId }};

  return (
    <SocketProvider url={socketUrl} options={socketOptions}>
      <Main />
    </SocketProvider>
  );
}

The useChannel hook lets you subscribe to a channel, and the useEventHandler hook lets you respond to incoming messages:

import React, { useContext, useEffect, useRef, useState } from 'react';
import { sendMessage, useChannel, useEventHandler } from '@ericlathrop/phoenix-js-react-hooks';

export function Main() {
  const [messages, setMessages] = useState([]);

  const room = 'cute kitties';
  const { channel: chatChannel } = useChannel('chat:' + room, undefined, (channel, { messages: initialMessages}) => {
    setMessages(initialMessages);
  });

  useEventHandler(chatChannel, 'new_message', message => {
    setMessages(messages.slice(0).concat([message]));
  });

  return (
    <div className="chat-messages" >
      {messages.map(({ id, message }) => <ChatMessage key={id} message={message} />)}
    </div>
  );
}

A React Context.Provider that provides a socket value which is the Phoenix Socket object.

The url prop is passed as the endPoint argument to the Socket constructor, and the options prop is passed as the opts argument to the Socket constructor.

const channel = useChannel(topic, params, onJoin)

A React hook that connects to a Phoenix Channel and lets you send/receive events. useChannel returns the Phoenix Channel that you can use in useEventHandler or sendMessage.

The topic argument is passed as the topic argument to the Channel constructor, and the params argument is passed as the params argument to the Channel constructor. The onJoin(channel, message) argument is a function that is called when the channel is successfully joined.

useEventHandler(channel, event, handler)

A React hook that specifies an event handler function to to executed when an event is received.

The channel argument is the Phoenix Channel on which to listen for events.

The event argument is a string specifying the name of the event to listen for.

The handler(message3) argument is a function that will be called with the message every time a matching event is received.

const promise = sendMessage(channel, event, payload)

A function that sends an event on a Phoenix Channel. sendMessage returns a Promise that resolves to the reply message from the server, if any.

The channel argument is the Phoenix Channel on which to send the event.

The event argument is a string specifying the name of the event to send.

The payload argument is a JavaScript object containing the data for the event you are sending.