How to use the phoenix.Presence function in phoenix

To help you get started, we’ve selected a few phoenix examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github flow-typed / flow-typed / definitions / npm / phoenix_v1.4.x / test_phoenix_v1.4.x.js View on Github external
it('sets up presence correctly', () => {
    const socket = new Socket('/socket', { params: { userToken: '456' } });
    socket.connect();
    const channel = socket.channel('room:123', { token: 'xyz' });
    channel.join();
    const presence = new Presence(channel);

    // detect if user has joined for the 1st time or from another tab/device
    presence.onJoin((id, current, newPres) => {
      if (!current) {
        console.log('user has entered for the first time', newPres);
      } else {
        console.log('user additional presence', newPres);
      }
    });

    // detect if user has left from all tabs/devices, or is still present
    presence.onLeave((id, current, leftPres) => {
      if (current && current.metas && current.metas.length === 0) {
        console.log('user has left from all devices', leftPres);
      } else {
        console.log('user left from a device', leftPres);
github mozilla / hubs / src / utils / hub-channel.js View on Github external
// Unbind presence, and then set up bindings after reconnect
    if (this.presence) {
      presenceBindings = {
        onJoin: this.presence.caller.onJoin,
        onLeave: this.presence.caller.onLeave,
        onSync: this.presence.caller.onSync
      };

      this.presence.onJoin(function() {});
      this.presence.onLeave(function() {});
      this.presence.onSync(function() {});
    }

    this.channel = await migrateChannelToSocket(this.channel, socket, params);
    this.presence = new Presence(this.channel);

    if (presenceBindings) {
      this.presence.onJoin(presenceBindings.onJoin);
      this.presence.onLeave(presenceBindings.onLeave);
      this.presence.onSync(presenceBindings.onSync);
    }
  }
github youzan / show-me-the-code / client / app / connection.service.ts View on Github external
join(roomId: string, username: string): Promise {
    this.username = username;
    if (this.channel$.getValue() !== null) {
      return Promise.resolve();
    }
    const socket = this.getSocket(username);
    const channel = socket.channel(`room:${roomId}`);
    const links = linkEvents(EVENTS, channel, this as EventEmitter);
    const presence = new Presence(channel);
    presence.onSync(() => {
      const userList = presence.list(pickMeta);
      this.userList$.next(userList);
      this.userMap.clear();
      for (let i = 0; i < userList.length; i += 1) {
        const user = userList[i];
        this.userMap.set(user.id, user);
      }
    });
    presence.onLeave(userId => this.emit('user.leave', { userId }));
    return new Promise((resolve, reject) => {
      channel
        .join()
        .receive('ok', ({ userId }: { userId: string }) => {
          this.roomId = roomId;
          this.userId = userId;
github mozilla / hubs / src / utils / hub-channel.js View on Github external
setPhoenixChannel = channel => {
    this.channel = channel;
    this.presence = new Presence(channel);
  };
github stride-nyc / remote_retro / web / static / js / services / retro_channel.js View on Github external
applyListenersWithDispatch(store, actions) {
    const {
      addIdea,
      retroUpdateCommitted,
      setPresences,
      updateIdea,
      deleteIdea,
      voteSubmission,
      updatePresence,
      voteRetraction,
    } = actions

    const { client } = this

    const presence = new Presence(client)

    presence.onSync(() => {
      const users = presence.list((_token, presence) => (presence.user))
      setPresences(users)
    })

    client.on("idea_committed", addIdea)

    client.on("retro_edited", retroUpdateCommitted)

    client.on("idea_edit_state_enabled", ({ id }) => {
      updateIdea(id, { inEditState: true, isLocalEdit: false })
    })

    client.on("idea_edit_state_disabled", disabledIdea => {
      updateIdea(disabledIdea.id, { inEditState: false, liveEditText: null })
github pinda-fun / pinda-fun / Web / src / components / room / database / phoenix / PhoenixDatabase.ts View on Github external
constructor(channel: Channel) {
    this.presence = new Presence(channel);
    this.onSyncHandler = () => { };
    this.hostId = null;
    this.oldHostMeta = null;

    this.presence.onSync(() => {
      const { state } = this.presence;
      if (this.hostId == null) {
        const maybeHostId = Object.keys(state).find((clientId) => state[clientId].metas[0].isHost);
        this.hostId = maybeHostId == null ? null : maybeHostId;
      }
      this.onSyncHandler(this.oldHostMeta);
      this.oldHostMeta = this.getHostMeta();
    });
  }
github MozillaReality / hubs-discord-bot / src / reticulum.js View on Github external
constructor(channel) {
    super();
    this.channel = channel;
    this.presence = new phoenix.Presence(channel);
  }